import "./file-picker.css";
import "core-js/modules/web.dom.iterable";
import _Promise from "@babel/runtime-corejs2/core-js/promise";

/* script */
//
//
//
//
//
//
//
import { componentMixin, createComponentName } from '../_utils/component';

class FileWrap {
  constructor(file) {
    this.rawFile = file;
    this.name = file.name;
    this.type = file.type;
    this.size = file.size;
    this.result = null;
  }

  readAs(type = 'blob') {
    return new _Promise((resolve, reject) => {
      const reader = new FileReader();
      const file = this.rawFile;
      const fnNamesMap = {
        default: 'readAsArrayBuffer',
        datauri: 'readAsDataURL',
        text: 'readAsText'
      };

      reader.onload = () => {
        this.result = reader.result;
        resolve(this.result);
      };

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

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

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

      url
    };
  }

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

}

const __vue_script__ = {
  name: createComponentName('file-picker'),
  mixins: [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: {
    async readFiles(files) {
      const type = this.readAs;

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

      await _Promise.all(files.map(file => {
        return file.readAs(type);
      }));
    },

    updateFiles(files) {
      const newFiles = [];

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

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

      this.files = newFiles;
    },

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

      inp.value = '';
      const 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 () {
  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 */

const __vue_inject_styles__ = undefined;
/* scoped */

const __vue_scope_id__ = undefined;
/* module identifier */

const __vue_module_identifier__ = undefined;
/* functional template */

const __vue_is_functional_template__ = false;
/* component normalizer */

import __vue_normalize__ from '../_utils/vue-normalize-component.js';
/* style inject */

/* style inject SSR */

export default __vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);