'use strict';

var iconsReact = require('@cloudtower/icons-react');
var core = require('@linaria/core');
var index$1 = require('../Button/index.js');
var index$4 = require('../Byte/index.js');
var index$2 = require('../Icon/index.js');
var index$5 = require('../Tooltip/index.js');
var index = require('../Typo/index.js');
var index$3 = require('../../coreX/OverflowTooltip/index.js');
var useParrotTranslation = require('../../hooks/useParrotTranslation.js');
var cs = require('classnames');
var _ = require('lodash');
var React = require('react');
var LocalUpload_hooks = require('./LocalUpload.hooks.js');
var LocalUpload_style = require('./LocalUpload.style.js');
var antd = require('antd');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var cs__default = /*#__PURE__*/_interopDefault(cs);
var ___default = /*#__PURE__*/_interopDefault(_);
var React__default = /*#__PURE__*/_interopDefault(React);

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const STATUS_ICON_MAP = {
  error: iconsReact.ExclamationErrorCircleFill16RedIcon,
  success: iconsReact.File16GradientBlueIcon,
  validating: iconsReact.Loading16GradientBlueIcon,
  "need-validate": iconsReact.Loading16GradientBlueIcon
};
const createBeforeUploadHandler = ({
  fileList,
  setFileList,
  validate,
  maxCount,
  isSingleSelect,
  setError,
  t,
  checkSingleSelectCount = false
}) => {
  return (file, _fileList) => {
    if (isSingleSelect) {
      if (checkSingleSelectCount && _fileList.length > 1) {
        setError(t("components.exceed_max_count", { count: 1 }) || "");
        return false;
      } else {
        setError("");
      }
      const _file2 = file;
      _file2.fileStatus = validate ? "need-validate" : "success";
      setFileList([_file2]);
      return false;
    }
    if (fileList.length + _fileList.length > maxCount) {
      setError(t("components.exceed_max_count", { count: maxCount }) || "");
    } else {
      setError("");
    }
    const _file = file;
    _file.fileStatus = validate ? "need-validate" : "success";
    const index = _fileList.findIndex((f) => f.uid === _file.uid);
    if (index !== -1) {
      const newList = ___default.default.uniqBy(
        [...fileList, ..._fileList.slice(0, index + 1)],
        "uid"
      );
      setFileList(newList);
    }
    return false;
  };
};
const UploadLabel = ({ className, children }) => {
  return /* @__PURE__ */ React__default.default.createElement("label", { className: core.cx("upload-label", index.Typo.Label.l2_regular, className) }, children);
};
const UploadDescription = ({ className, children }) => {
  return /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("upload-description", index.Typo.Label.l4_regular, className) }, children);
};
const UploadButton = ({
  children,
  hideIcon = false,
  className,
  fileList,
  setFileList,
  validate,
  maxCount,
  multiple = false,
  disabled,
  accept
}) => {
  const { t } = useParrotTranslation();
  const [error, setError] = React.useState("");
  const _maxCount = multiple ? maxCount || Infinity : 1;
  const isSingleSelect = _maxCount === 1;
  LocalUpload_hooks.useFileValidation({
    fileList,
    setFileList,
    validate,
    createNewFile: false
  });
  LocalUpload_hooks.useFileCountErrorClear({
    fileList,
    maxCount: _maxCount,
    setError
  });
  const props = {
    accept,
    fileList,
    showUploadList: false,
    beforeUpload: createBeforeUploadHandler({
      fileList,
      setFileList,
      validate,
      maxCount: _maxCount,
      isSingleSelect,
      setError,
      t,
      checkSingleSelectCount: false
    }),
    multiple
  };
  return /* @__PURE__ */ React__default.default.createElement(antd.Upload, __spreadProps(__spreadValues({}, props), { className: cs__default.default("upload-button", className) }), /* @__PURE__ */ React__default.default.createElement(
    index$1,
    {
      disabled,
      prefixIcon: !hideIcon ? /* @__PURE__ */ React__default.default.createElement(index$2, { src: iconsReact.Uploading16GradientGrayIcon }) : void 0,
      hoverPrefixIcon: !hideIcon ? /* @__PURE__ */ React__default.default.createElement(index$2, { src: iconsReact.Uploading16GradientBlueIcon }) : void 0
    },
    children
  ), error ? /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("upload-error", index.Typo.Label.l4_regular) }, error) : null);
};
const UploadDragger = ({
  children,
  className,
  fileList,
  setFileList,
  validate,
  maxCount,
  multiple = false,
  disabled,
  accept
}) => {
  var _a;
  const { t } = useParrotTranslation();
  const [error, setError] = React.useState("");
  const _maxCount = multiple ? maxCount || Infinity : 1;
  const isSingleSelect = _maxCount === 1;
  const reachMaxCount = !!maxCount && !isSingleSelect && fileList.length >= maxCount;
  LocalUpload_hooks.useFileValidation({
    fileList,
    setFileList,
    validate,
    createNewFile: true
  });
  LocalUpload_hooks.useFileCountErrorClear({
    fileList,
    maxCount: _maxCount,
    setError
  });
  const props = {
    accept,
    fileList,
    showUploadList: false,
    beforeUpload: createBeforeUploadHandler({
      fileList,
      setFileList,
      validate,
      maxCount: _maxCount,
      isSingleSelect,
      setError,
      t,
      checkSingleSelectCount: true
    }),
    disabled,
    multiple
  };
  const DefaultChildren = /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement(
    index$2,
    {
      src: (fileList == null ? void 0 : fileList.length) ? reachMaxCount ? iconsReact.Upload24GradientGrayIcon : iconsReact.Upload24GradientBlueIcon : iconsReact.Upload48GradientBlueIcon,
      iconHeight: (fileList == null ? void 0 : fileList.length) ? 24 : 48,
      iconWidth: (fileList == null ? void 0 : fileList.length) ? 24 : 48
    }
  ), /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("upload-drag-text", index.Typo.Label.l2_regular) }, t("components.upload_file_desc")));
  const fileStatus = isSingleSelect ? ((_a = fileList[0]) == null ? void 0 : _a.fileStatus) || "success" : void 0;
  const FileInfo = ({ file }) => {
    const fileStatus2 = file.fileStatus || "success";
    return /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement("div", { className: "file-info", onClick: (e) => e.preventDefault() }, /* @__PURE__ */ React__default.default.createElement(
      index$2,
      {
        src: STATUS_ICON_MAP[fileStatus2],
        isRotate: fileStatus2 === "validating" || fileStatus2 === "need-validate"
      }
    ), /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("file-name", index.Typo.Label.l4_regular) }, file.name || file.fileName)), /* @__PURE__ */ React__default.default.createElement(index$1, { className: index.Typo.Label.l2_regular, type: "link" }, t("components.reselect_file")));
  };
  const Error2 = () => {
    if (error) {
      return /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("upload-error", index.Typo.Label.l4_regular) }, error);
    }
    const file = fileList[0];
    if (isSingleSelect && (file == null ? void 0 : file.fileStatus) === "error" && file.error) {
      return /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("upload-error", index.Typo.Label.l4_regular) }, file.error);
    }
    return null;
  };
  return /* @__PURE__ */ React__default.default.createElement("div", { className: cs__default.default("upload-drag", className) }, /* @__PURE__ */ React__default.default.createElement(
    antd.Upload.Dragger,
    __spreadProps(__spreadValues({}, props), {
      disabled: disabled || fileStatus === "validating",
      className: cs__default.default(
        "upload-drag-area",
        fileList.length ? "has-file" : "",
        reachMaxCount && "reach-max-count",
        isSingleSelect && "single",
        fileStatus === "error" && "file-error",
        fileStatus === "validating" && "file-validating"
      )
    }),
    isSingleSelect && fileList.length ? /* @__PURE__ */ React__default.default.createElement(FileInfo, { file: fileList[0] }) : children || DefaultChildren
  ), /* @__PURE__ */ React__default.default.createElement(Error2, null));
};
const UploadFileInfo = ({
  file,
  removeFile,
  disabled,
  onRemove
}) => {
  const fileStatus = file.fileStatus || "success";
  const handleRemove = () => {
    var _a;
    removeFile((_a = file.uid) != null ? _a : "");
    if (onRemove && "fileStatus" in file) {
      onRemove(file);
    }
  };
  return /* @__PURE__ */ React__default.default.createElement(
    "div",
    {
      className: cs__default.default(LocalUpload_style.FileInfoWrapperStyle, {
        "file-error-wrapper": fileStatus === "error",
        disabled
      })
    },
    /* @__PURE__ */ React__default.default.createElement(
      index$2,
      {
        src: STATUS_ICON_MAP[fileStatus],
        isRotate: fileStatus === "validating" || fileStatus === "need-validate"
      }
    ),
    /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("file-info", index.Typo.Label.l4_regular) }, /* @__PURE__ */ React__default.default.createElement(
      index$3,
      {
        className: "file-name",
        content: file.name || file.fileName,
        tooltip: file.name || file.fileName
      }
    ), file.size ? /* @__PURE__ */ React__default.default.createElement("div", { className: "file-info" }, /* @__PURE__ */ React__default.default.createElement(
      index$4,
      {
        rawValue: file.size,
        valueClassName: "file-size",
        unitClassName: "file-size-unit"
      }
    )) : /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null), file.fileStatus === "error" && !!file.error && /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("upload-file-error", index.Typo.Footnote.f2_regular) }, file.error)),
    /* @__PURE__ */ React__default.default.createElement(
      index$2,
      {
        src: iconsReact.XmarkRemove16SecondaryIcon,
        onClick: disabled ? void 0 : handleRemove
      }
    )
  );
};
const FileListItem = ({ file, removeFile, onRemove }) => {
  const fileStatus = file.fileStatus || "success";
  const { t } = useParrotTranslation();
  const handleRemove = (e) => {
    e.stopPropagation();
    removeFile(file.uid);
    onRemove == null ? void 0 : onRemove(file);
  };
  return /* @__PURE__ */ React__default.default.createElement(
    "div",
    {
      className: core.cx("upload-file-item", index.Typo.Label.l4_regular),
      key: file.uid
    },
    /* @__PURE__ */ React__default.default.createElement("div", { className: "upload-file-info" }, /* @__PURE__ */ React__default.default.createElement("div", { className: "file-info" }, /* @__PURE__ */ React__default.default.createElement(
      index$2,
      {
        src: STATUS_ICON_MAP[fileStatus],
        isRotate: fileStatus === "validating" || fileStatus === "need-validate"
      }
    ), /* @__PURE__ */ React__default.default.createElement(
      "div",
      {
        className: fileStatus === "validating" || fileStatus === "need-validate" ? "validating" : ""
      },
      file.name || file.fileName
    )), /* @__PURE__ */ React__default.default.createElement(index$5, { title: t("common.remove") }, /* @__PURE__ */ React__default.default.createElement(
      index$2,
      {
        className: "remove-button",
        src: iconsReact.XmarkRemove24SecondaryIcon,
        onClick: handleRemove
      }
    ))),
    file.fileStatus === "error" && !!file.error && /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("upload-file-error", index.Typo.Footnote.f2_regular) }, file.error)
  );
};
const UploadFileList = ({
  className,
  fileList,
  removeFile,
  type = "list",
  disableRemove = false,
  onRemove
}) => {
  if (!fileList.length) {
    return null;
  }
  return /* @__PURE__ */ React__default.default.createElement(
    "div",
    {
      className: cs__default.default(
        "upload-file-list",
        type === "list" ? "" : "upload-file-info-list",
        className
      )
    },
    fileList.map(
      (file) => type === "list" ? /* @__PURE__ */ React__default.default.createElement(
        FileListItem,
        {
          key: file.uid,
          file,
          removeFile,
          onRemove
        }
      ) : /* @__PURE__ */ React__default.default.createElement(
        UploadFileInfo,
        {
          key: file.uid,
          file,
          removeFile,
          disabled: disableRemove,
          onRemove
        }
      )
    )
  );
};

exports.FileListItem = FileListItem;
exports.UploadButton = UploadButton;
exports.UploadDescription = UploadDescription;
exports.UploadDragger = UploadDragger;
exports.UploadFileInfo = UploadFileInfo;
exports.UploadFileList = UploadFileList;
exports.UploadLabel = UploadLabel;
