"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));

require("regenerator-runtime/runtime");

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator"));

require("./file-picker.css");

require("core-js/modules/web.dom.iterable");

var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));

var _base = require("../../utils/base");

var _vueNormalizeComponent = _interopRequireDefault(require("../../utils/vue-normalize-component.js"));

/* script */
//
//
//
//
//
//
//
var FileWrap =
/*#__PURE__*/
function () {
  function FileWrap(file) {
    this.rawFile = file;
    this.name = file.name;
    this.type = file.type;
    this.size = file.size;
    this.result = null;
  }

  var _proto = FileWrap.prototype;

  _proto.readAs = function readAs(type) {
    var _this = this;

    if (type === void 0) {
      type = 'blob';
    }

    return new _promise.default(function (resolve, reject) {
      var reader = new FileReader();
      var file = _this.rawFile;
      var fnNamesMap = {
        default: 'readAsArrayBuffer',
        datauri: 'readAsDataURL',
        text: 'readAsText'
      };

      reader.onload = function () {
        _this.result = reader.result;
        resolve(_this.result);
      };

      reader.onerror = function () {
        reject(new Error('File read error'));
      };

      var fnName = fnNamesMap[type] || fnNamesMap.default;
      reader[fnName](file);
    });
  };

  _proto.createURL = function createURL() {
    var url = URL.createObjectURL(this.rawFile);
    return {
      destroy() {
        URL.revokeObjectURL(url);
      },

      url
    };
  };

  _proto.toJSON = function toJSON() {
    return {
      name: this.name,
      type: this.type,
      size: this.size,
      result: this.result
    };
  };

  return FileWrap;
}();

var __vue_script__ = {
  name: (0, _base.createComponentName)('file-picker'),
  mixins: [_base.componentMixin],
  props: {
    value: {
      type: Array,

      default() {
        return [];
      }

    },
    multiple: {
      type: Boolean,
      default: false
    },
    append: {
      type: Boolean,
      default: true
    },
    accept: {
      type: String,
      default: 'image/jpeg,image/png,image/gif'
    },
    title: {
      type: String,
      default: '上传'
    },
    label: {
      type: String,
      default: ''
    },
    readAs: {
      type: String,
      default: null
    }
  },

  data() {
    return {
      currentStatus: 1,
      dialogShow: false,
      externalImg: '',
      // external img url,
      qrcodeInterval: null,
      imgInterval: null,
      qrcode_token: '',
      qrcode_url: '',
      qrcodeOption: {
        size: 148
      },
      scaned: false,
      // qrcode scaned,
      imgRquest: null
    };
  },

  computed: {
    files: {
      get() {
        return this.value;
      },

      set(files) {
        this.$emit('input', files);
      }

    }
  },
  methods: {
    readFiles(files) {
      var _this2 = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee() {
        var type;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                type = _this2.readAs;

                if (!files) {
                  files = _this2.files;
                }

                _context.next = 4;
                return _promise.default.all(files.map(function (file) {
                  return file.readAs(type);
                }));

              case 4:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }))();
    },

    updateFiles(files) {
      var newFiles = [];

      if (this.multiple) {
        if (this.append) {
          newFiles.push.apply(newFiles, this.files);
        }

        newFiles.push.apply(newFiles, files);
      } else {
        newFiles.push(files[0]);
      }

      this.files = newFiles;
    },

    onFileChange(e) {
      var inp = e.target;
      var files = [].concat(inp.files).map(function (file) {
        return new FileWrap(file);
      }); // Reset input

      inp.value = '';
      var file = files[0];

      if (!file || !file.size) {
        return;
      }

      if (this.readAs) {
        this.readFiles(files);
      }

      this.updateFiles(files);
      this.$emit('change', file, e);
    }

  }
};
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c("label", {
    class: _vm.bem(),
    attrs: {
      title: _vm.title
    }
  }, [_c("input", {
    attrs: {
      type: "file",
      multiple: _vm.multiple,
      accept: _vm.accept
    },
    on: {
      change: _vm.onFileChange
    }
  }), _vm._v(" "), _c("span", [_vm._v(_vm._s(_vm.label || _vm.title))])]);
};

var __vue_staticRenderFns__ = [];
__vue_render__._withStripped = true;
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* component normalizer */

/* style inject */

/* style inject SSR */
var _default = (0, _vueNormalizeComponent.default)({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

exports.default = _default;