'use strict';

Object.defineProperty(exports, '__esModule', {
  value: true
});

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var _globalWindow = require('global/window');

var _globalWindow2 = _interopRequireDefault(_globalWindow);

var _aesDecrypter = require('aes-decrypter');

/**
 * Our web wroker interface so that things can talk to aes-decrypter
 * that will be running in a web worker. the scope is passed to this by
 * webworkify.
 *
 * @param {Object} self the scope for the web worker
 */
var Worker = function Worker(self) {
  self.onmessage = function (event) {
    var data = event.data;

    if (data.action === 'decrypt') {
      var encrypted = new Uint8Array(data.encrypted.bytes, data.encrypted.byteOffset, data.encrypted.byteLength);
      var key = new Uint32Array(data.key.bytes, data.key.byteOffset, data.key.byteLength / 4);
      var iv = new Uint32Array(data.iv.bytes, data.iv.byteOffset, data.iv.byteLength / 4);
      var fn = function fn(err, bytes) {
        if (err) {
          return;
        }

        _globalWindow2['default'].postMessage({
          action: 'done',
          bytes: bytes.buffer,
          byteOffset: bytes.byteOffset,
          byteLength: bytes.byteLength
        }, [bytes.buffer]);
      };

      return new _aesDecrypter.Decrypter(encrypted, key, iv, fn);
    }
  };
};

exports['default'] = function (self) {
  return new Worker(self);
};

module.exports = exports['default'];