'use strict';

var icons = require('@ant-design/icons');
var index$1 = require('../Button/index.js');
var index = require('../KitStoreProvider/index.js');
var index$3 = require('../Steps/index.js');
var index$2 = require('../Styled/index.js');
var useLegacyComponentWarning = require('../../hooks/useLegacyComponentWarning.js');
var useParrotTranslation = require('../../hooks/useParrotTranslation.js');
var antd = require('antd');
var cs = require('classnames');
var React = require('react');
var modal = require('../../store/modal.js');

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

var cs__default = /*#__PURE__*/_interopDefault(cs);
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));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
const LegacyModal = (props) => {
  var _b;
  const { t } = useParrotTranslation();
  useLegacyComponentWarning(
    "Warning: LegacyModal is deprecated and will be removed in future versions. Please use Dialog/MediumDialog/SmallDialog/ImmersiveDialog/WizardDialog instead."
  );
  const _a = props, {
    error,
    okText = t("common.confirm"),
    cancelText = t("common.cancel"),
    confirmLoading,
    okButtonProps,
    onOk,
    cancelButtonProps,
    onCancel,
    maskClosable = false,
    normal = true,
    className,
    fullscreen,
    wizard,
    footer,
    width,
    showCancel = true,
    showOk = true,
    afterClose,
    closeIcon,
    hideFooterButtonBorder = true
  } = _a, restProps = __objRest(_a, [
    "error",
    "okText",
    "cancelText",
    "confirmLoading",
    "okButtonProps",
    "onOk",
    "cancelButtonProps",
    "onCancel",
    "maskClosable",
    "normal",
    "className",
    "fullscreen",
    "wizard",
    "footer",
    "width",
    "showCancel",
    "showOk",
    "afterClose",
    "closeIcon",
    "hideFooterButtonBorder"
  ]);
  const stack = index.useKitSelector(
    (state) => state.modal.stack
  );
  const id = index.useKitSelector(
    (state) => state.modal.closeId
  );
  const dispatch = index.useKitDispatch();
  const idRef = React.useRef((_b = stack[stack.length - 1]) == null ? void 0 : _b.id);
  const transitionClass = React.useRef(fullscreen ? "fullscreen-modal" : "modal-zoom");
  const confirmText = React.useMemo(() => {
    let text = (okButtonProps == null ? void 0 : okButtonProps.children) || okText;
    if (wizard && typeof wizard === "object" && wizard.steps[wizard.step]) {
      const { step, steps } = wizard;
      text = step < steps.length - 1 ? steps[step].okText || t("common.next_step") : steps[step].okText || text;
    }
    return text;
  }, [okButtonProps == null ? void 0 : okButtonProps.children, okText, t, wizard]);
  const prevText = React.useMemo(() => {
    let text = /* @__PURE__ */ React__default.default.createElement("span", null, "< ", t("common.prev_step"));
    if (wizard && typeof wizard === "object" && wizard.steps[wizard.step]) {
      const { step, steps } = wizard;
      if (typeof steps[step].prevText === "string") {
        text = /* @__PURE__ */ React__default.default.createElement("span", null, "< ", steps[step].prevText);
      } else if (steps[step].prevText !== void 0) {
        text = steps[step].prevText;
      }
    }
    return text;
  }, [t, wizard]);
  const stepsRef = React.useRef([]);
  if (wizard && typeof wizard === "object" && !wizard.destroyOtherStep && wizard.steps[wizard.step]) {
    stepsRef.current[wizard.step] = wizard.steps[wizard.step].render;
  }
  return /* @__PURE__ */ React__default.default.createElement(
    antd.Modal,
    __spreadProps(__spreadValues({
      maskClosable,
      className: cs__default.default(
        className,
        fullscreen && "fullscreen",
        fullscreen && wizard && "wizard",
        !fullscreen && normal && "normal-modal"
      ),
      width: fullscreen ? "calc(100vw - 16px)" : width || (normal ? 460 : ""),
      onCancel: (e) => {
        onCancel == null ? void 0 : onCancel(e);
        transitionClass.current = fullscreen ? "" : "modal-zoom";
      },
      closeIcon: closeIcon != null ? closeIcon : /* @__PURE__ */ React__default.default.createElement(icons.CloseCircleFilled, null),
      destroyOnClose: true,
      focusTriggerAfterClose: false,
      transitionName: transitionClass.current
    }, restProps), {
      afterClose: () => {
        afterClose == null ? void 0 : afterClose();
        dispatch({ type: modal.ModalActions.REMOVE_MODAL, id: idRef.current });
      },
      visible: idRef.current !== id,
      footer: /* @__PURE__ */ React__default.default.createElement(
        "div",
        {
          className: cs__default.default(
            "footer-content",
            hideFooterButtonBorder && "no-button-border"
          )
        },
        footer === void 0 ? /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement("div", { className: "modal-footer-left" }, wizard && typeof wizard === "object" && !wizard.disablePrevStep && wizard.step !== 0 && /* @__PURE__ */ React__default.default.createElement(
          "span",
          {
            className: "prev-step",
            onClick: () => {
              var _a2, _b2, _c, _d;
              if (typeof wizard === "object" && ((_a2 = wizard.steps[wizard.step]) == null ? void 0 : _a2.onPrev)) {
                (_c = (_b2 = wizard.steps[wizard.step]).onPrev) == null ? void 0 : _c.call(_b2);
                return;
              }
              (_d = wizard.onStepChange) == null ? void 0 : _d.call(wizard, wizard.step - 1);
            }
          },
          prevText
        ), error && /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement("span", { className: "modal-error" }, error))), /* @__PURE__ */ React__default.default.createElement("div", { className: "modal-footer-btn-group" }, showCancel && /* @__PURE__ */ React__default.default.createElement(
          index$1,
          __spreadValues({
            type: "quiet",
            onMouseDown: (e) => {
              e.preventDefault();
            },
            onClick: (e) => {
              onCancel == null ? void 0 : onCancel(e);
              transitionClass.current = fullscreen ? "" : "modal-zoom";
            },
            className: cs__default.default(cancelButtonProps == null ? void 0 : cancelButtonProps.className)
          }, cancelButtonProps),
          cancelText
        ), showOk && /* @__PURE__ */ React__default.default.createElement(
          index$1,
          __spreadValues({
            onClick: (e) => {
              var _a2, _b2;
              onOk == null ? void 0 : onOk(e);
              if (typeof wizard === "object" && wizard.steps[wizard.step]) {
                (_b2 = (_a2 = wizard.steps[wizard.step]).onOk) == null ? void 0 : _b2.call(_a2, e);
              }
              transitionClass.current = fullscreen ? "" : "modal-send";
            },
            type: "primary",
            loading: confirmLoading
          }, okButtonProps),
          confirmText
        ))) : footer
      )
    }),
    wizard && typeof wizard === "object" ? /* @__PURE__ */ React__default.default.createElement(index$2.WizardBody, null, /* @__PURE__ */ React__default.default.createElement("div", { className: "left" }, wizard.hideLeft ? null : /* @__PURE__ */ React__default.default.createElement(
      index$3,
      {
        current: wizard.step,
        onChange: (value) => {
          var _a2;
          return (_a2 = wizard.onStepChange) == null ? void 0 : _a2.call(wizard, value);
        },
        direction: "vertical",
        stepsConfig: wizard.steps.map((s) => ({
          title: s.title,
          disabled: s.disabled
        }))
      }
    )), /* @__PURE__ */ React__default.default.createElement("div", { className: "middle" }, restProps.children, wizard.destroyOtherStep ? wizard.steps[wizard.step].render : stepsRef.current.map((step, index) => /* @__PURE__ */ React__default.default.createElement(
      "div",
      {
        key: index,
        style: {
          display: index === wizard.step ? "block" : "none"
        }
      },
      step
    ))), wizard.right && /* @__PURE__ */ React__default.default.createElement("div", { className: "right" }, wizard.right)) : restProps.children
  );
};
var LegacyModal$1 = LegacyModal;

module.exports = LegacyModal$1;
