function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import React, { useMemo } from 'react';
import { isNumber } from '@pisell/utils';
import { withMode } from "../../dataSourceForm/utils";
import ReadPretty from "./ReadPretty";
import BaseUpload from "./BaseUpload";
import { useMediaTypes, useMediaConfig } from "./serve";
import { useIsShop } from "../../hooks/usePlatform";
var Upload = function Upload(props) {
  var isShop = useIsShop();
  var presetType = props.presetType,
    propsMediaConfig = props.mediaConfig,
    propsPresetTypeString = props.presetTypeString;
  var _useMediaTypes = useMediaTypes(),
    getExtensionsByIds = _useMediaTypes.getExtensionsByIds,
    getMaxSizeByIds = _useMediaTypes.getMaxSizeByIds;
  var _useMediaConfig = useMediaConfig(propsMediaConfig),
    mediaConfig = _useMediaConfig.data;
  var presetTypeString = isShop ? propsPresetTypeString : getExtensionsByIds(presetType || []);
  var maxSizeMap = getMaxSizeByIds(presetType || []);
  var maxCount = useMemo(function () {
    var minArr = [];
    if (isNumber(mediaConfig === null || mediaConfig === void 0 ? void 0 : mediaConfig.upload_onetime_limit)) {
      minArr.push(mediaConfig === null || mediaConfig === void 0 ? void 0 : mediaConfig.upload_onetime_limit);
    }
    if (isNumber(props.maxCount)) {
      minArr.push(props.maxCount);
    }
    return Math.min.apply(Math, minArr);
  }, [mediaConfig, props.maxCount]);
  var maxSize = useMemo(function () {
    var minArr = [];
    if (isNumber(mediaConfig === null || mediaConfig === void 0 ? void 0 : mediaConfig.upload_maxsize)) {
      minArr.push(mediaConfig === null || mediaConfig === void 0 ? void 0 : mediaConfig.upload_maxsize);
    }
    if (isNumber(props.maxSize)) {
      minArr.push(props.maxSize);
    }
    return Math.min.apply(Math, minArr);
  }, [mediaConfig, props.maxSize]);
  return /*#__PURE__*/React.createElement(BaseUpload, _extends({}, props, {
    maxCount: maxCount,
    maxSize: maxSize,
    presetType: presetTypeString,
    typeMaxSizeMap: maxSizeMap
  }));
};
var UploadWithMode = withMode(Upload, ReadPretty);
export default UploadWithMode;