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; }

import { __awaiter } from "tslib";
import * as React from 'react';
import { View, Image } from 'remax/one';
import { previewImage, chooseImage } from '../one';
import { sync, to, deepClone } from './../_utils';
import { getPrefixCls } from './../common';
import Icon from '../icon';
var prefixCls = 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;
      });
    }

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

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

export default ImageUpload;