"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _upload = _interopRequireDefault(require("@alifd/next/lib/upload"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _message = _interopRequireDefault(require("@alifd/next/lib/message"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireWildcard(require("react"));
var _coreLibBeta = require("@cverse/core-lib-beta");
var _UploadBatch = require("./UploadBatch");
require("./index.less");
var _locale = require("../../../src/locale");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var _ = require('lodash');
var CUploadBatchSetter = function CUploadBatchSetter(props) {
  var uploadBatch = new _UploadBatch.UploadBatch('617');
  var value = props.value,
    onChange = props.onChange;
  var _useState = (0, _react.useState)(value || []),
    imgList = _useState[0],
    setImgList = _useState[1];
  var _useState2 = (0, _react.useState)(false),
    loading = _useState2[0],
    setLoading = _useState2[1];
  var uploadImage = /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee(file, index) {
      var result;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return uploadBatch.uploadImage(file);
          case 2:
            result = _context.sent;
            if (result) {
              setImgList(imgList[index].url = result);
              _message["default"].success((0, _locale.t)('tool-video.key_YlYhgW2xsucc'));
            } else {
              _message["default"].success((0, _locale.t)('tool-video.key_YlYhW2failed'));
            }
            setLoading(false);
          case 5:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return function uploadImage(_x, _x2) {
      return _ref.apply(this, arguments);
    };
  }();
  var onSelect = /*#__PURE__*/function () {
    var _ref2 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2(files) {
      var sortArr, nameArr, result, urlArr;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            _context2.prev = 0;
            setLoading(true);
            sortArr = files.sort(function (a, b) {
              return (0, _UploadBatch.fileNameCompare)(a.name, b.name);
            });
            nameArr = sortArr.map(function (item) {
              return {
                url: '',
                name: item.name.replace(/\.png$/, '')
              };
            });
            _context2.next = 6;
            return uploadBatch.uploadImage(sortArr);
          case 6:
            result = _context2.sent;
            if (result) {
              urlArr = nameArr.map(function (item, index) {
                return {
                  url: result[index],
                  name: item.name
                };
              }); // const deepCopy = _.cloneDeep(urlArr);
              setImgList([].concat(imgList, urlArr));
              _message["default"].success((0, _locale.t)('tool-video.key_YlYhgW2xsucc'));
            } else {
              _message["default"].error((0, _locale.t)('tool-video.key_YlYhW2failed'));
            }
            setLoading(false);
            _context2.next = 15;
            break;
          case 11:
            _context2.prev = 11;
            _context2.t0 = _context2["catch"](0);
            setLoading(false);
            console.error('Error during file upload:', _context2.t0);
            // 处理错误逻辑
          case 15:
          case "end":
            return _context2.stop();
        }
      }, _callee2, null, [[0, 11]]);
    }));
    return function onSelect(_x3) {
      return _ref2.apply(this, arguments);
    };
  }();
  var moveUp = function moveUp(index) {
    if (index > 0 && index < imgList.length) {
      var newImgList = [].concat(imgList); // 创建副本
      var temp = newImgList[index - 1];
      newImgList[index - 1] = newImgList[index];
      newImgList[index] = temp;
      setImgList(newImgList);
    }
  };
  var moveDown = function moveDown(index) {
    if (index >= 0 && index < imgList.length - 1) {
      var newImgList = [].concat(imgList); // 创建副本
      var temp = newImgList[index + 1];
      newImgList[index + 1] = newImgList[index];
      newImgList[index] = temp;
      setImgList(newImgList);
    }
  };
  (0, _react.useEffect)(function () {
    onChange(imgList);
  }, [imgList]);
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_upload["default"], {
    multiple: true,
    autoUpload: false,
    onSelect: onSelect
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    className: "upLoad",
    loading: loading
  }, !loading && /*#__PURE__*/_react["default"].createElement(_icon["default"], {
    type: "upload"
  }))), /*#__PURE__*/_react["default"].createElement("div", null, imgList.length > 0 && /*#__PURE__*/_react["default"].createElement("div", {
    className: "video-frames-body"
  }, imgList.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "body-item",
      style: {
        cursor: 'pointer'
      }
    }, /*#__PURE__*/_react["default"].createElement("img", {
      src: item.url,
      alt: ""
    }), /*#__PURE__*/_react["default"].createElement("span", null, (0, _coreLibBeta.hideMoreText)(item.name, 4, 6) || index + 1), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: "arrow-up",
      onClick: function onClick() {
        moveUp(index);
      },
      size: 'small'
    }), /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: "arrow-down",
      onClick: function onClick() {
        moveDown(index);
      },
      size: 'small'
    })), /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: "close",
      size: 'small',
      onClick: function onClick() {
        setImgList(imgList.filter(function (value) {
          return value != item;
        }));
      }
    }));
  })), imgList.length > 0 && /*#__PURE__*/_react["default"].createElement("div", {
    className: "clear"
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "num"
  }, (0, _locale.t)('tool-video.key_YlYhW2fitotal'), " ", imgList.length, " ", (0, _locale.t)('tool-video.key_YlYhW2fizhang')), /*#__PURE__*/_react["default"].createElement("span", {
    className: "num"
  }, (0, _locale.t)('tool-video.key_YlYhW2cdelerr'), " ", imgList.filter(function (item) {
    return item === null;
  }).length, " ", (0, _locale.t)('tool-video.key_YlYhW2fizhang')), /*#__PURE__*/_react["default"].createElement(_button["default"], {
    onClick: function onClick() {
      setImgList([]);
    }
  }, (0, _locale.t)('tool-video.key_YlYhW2ficlear')))));
};
var _default = exports["default"] = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  (0, _inheritsLoose2["default"])(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/_react["default"].createElement(CUploadBatchSetter, this.props);
  };
  return _default;
}(_react.Component);