import { Component, Mixins } from 'vue-property-decorator';

@Component({})
export default class SandboxComponent extends Mixins() {
  private uploadHandler(data: any, done: () => void): void {
    this.$store
      .dispatch('utils/cryptoPro/fetchCertificates')
      .then((certificates) => {
        this.$store
          .dispatch('utils/cryptoPro/generateHash', {
            file: data.files[0],
          })
          .then((hash) => {
            this.$store
              .dispatch('utils/cryptoPro/createSignature', {
                hash,
                certificate: certificates[1],
              })
              .then((signature) => {
                function download(data: any, filename: any, type: any) {
                  const file = new Blob([data], { type: type });
                  if (window.navigator.msSaveOrOpenBlob)
                    // IE10+
                    window.navigator.msSaveOrOpenBlob(file, filename);
                  else {
                    // Others
                    const a = document.createElement('a');
                    const url = URL.createObjectURL(file);
                    a.href = url;
                    a.download = filename;
                    document.body.appendChild(a);
                    a.click();
                    setTimeout(function () {
                      document.body.removeChild(a);
                      window.URL.revokeObjectURL(url);
                    }, 0);
                  }
                }
                download(signature, `${data.files[0].name}.sig`, 'text/plain');

                done();
              });
          });
      });
  }
}
