"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _tslib = require("tslib");

var React = _interopRequireWildcard(require("react"));

var _one = require("remax/one");

var _one2 = require("../one");

var _utils = require("./../_utils");

var _common = require("./../common");

var _icon = _interopRequireDefault(require("../icon"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var prefixCls = (0, _common.getPrefixCls)('image-upload');

var ImageUpload = function ImageUpload(props) {
  var _props$files = props.files,
      files = _props$files === void 0 ? [] : _props$files,
      onChange = props.onChange,
      multiple = props.multiple,
      multipleCount = props.multipleCount,
      sizeType = props.sizeType,
      sourceType = props.sourceType,
      _props$deletable = props.deletable,
      deletable = _props$deletable === void 0 ? true : _props$deletable,
      disabled = props.disabled,
      maxCount = props.maxCount,
      children = props.children;

  var handleClickImage = function handleClickImage(index) {
    var urls = files;
    var current = index;

    if (typeof files[index] !== 'string') {
      urls = files.map(function (i) {
        return i.url;
      });
    }

    (0, _one2.previewImage)({
      urls: urls,
      current: current,
      // @ts-ignore
      enablesavephoto: true,
      enableShowPhotoDownload: true
    });
  };

  var handleAdd = function handleAdd() {
    return (0, _tslib.__awaiter)(void 0, void 0, void 0, /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var params, _yield$to, _yield$to2, errc, resc, targetFiles, newFiles;

      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!disabled) {
                _context.next = 2;
                break;
              }

              return _context.abrupt("return");

            case 2:
              params = {};

              if (multiple) {
                params.multiple = true;
                params.count = 99;
              }

              if (multipleCount) {
                params.count = multipleCount;
              }

              if (sizeType) {
                params.sizeType = sizeType;
              }

              if (sourceType) {
                params.sourceType = sourceType;
              }

              _context.next = 9;
              return (0, _utils.to)((0, _utils.sync)(_one2.chooseImage, params));

            case 9:
              _yield$to = _context.sent;
              _yield$to2 = _slicedToArray(_yield$to, 2);
              errc = _yield$to2[0];
              resc = _yield$to2[1];

              if (!errc) {
                _context.next = 15;
                break;
              }

              return _context.abrupt("return");

            case 15:
              targetFiles = resc.filePaths ? resc.filePaths.map(function (i) {
                return i;
              }) : resc.tempFilePaths.map(function (i) {
                return i;
              });
              newFiles = files.concat(targetFiles);
              onChange === null || onChange === void 0 ? void 0 : onChange(newFiles);

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

  var handleDelete = function handleDelete(e, index) {
    e.stopPropagation();
    var newValue = (0, _utils.deepClone)(files);
    newValue.splice(index, 1);
    newValue = newValue.map(function (item, index) {
      var newItem = item;

      if (typeof newItem === 'string') {
        return newItem;
      }

      newItem.key = String(index);
      return newItem;
    });
    onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
  };

  return /*#__PURE__*/React.createElement(_one.View, {
    className: prefixCls
  }, files.map(function (item, index) {
    return /*#__PURE__*/React.createElement(_one.View, {
      key: item.key || index,
      className: "".concat(prefixCls, "-item"),
      onTap: function onTap() {
        return handleClickImage(index);
      }
    }, deletable ? /*#__PURE__*/React.createElement(_one.View, {
      className: "".concat(prefixCls, "-item-delete"),
      onTap: function onTap(e) {
        handleDelete(e, index);
      }
    }, /*#__PURE__*/React.createElement(_icon.default, {
      type: "close",
      size: 12,
      color: "#FDFFFD",
      style: {
        width: '24px',
        height: '24px',
        lineHeight: '24px',
        textAlign: 'center',
        verticalAlign: 'text-top'
      }
    })) : null, /*#__PURE__*/React.createElement(_one.Image, {
      mode: "widthFix",
      src: item.url || item
    }));
  }), !maxCount || files.length < maxCount ? /*#__PURE__*/React.createElement(_one.View, {
    onTap: handleAdd
  }, children !== null && children !== void 0 ? children : /*#__PURE__*/React.createElement(_one.View, {
    className: "".concat(prefixCls, "-add")
  }, /*#__PURE__*/React.createElement(_icon.default, {
    type: "add",
    size: "24px",
    color: "#BABEC6"
  }))) : null);
};

var _default = ImageUpload;
exports.default = _default;