'use strict';

var iconsReact = require('@cloudtower/icons-react');
var core = require('@linaria/core');
var index = require('../KitStoreProvider/index.js');
var useParrotTranslation = require('../../hooks/useParrotTranslation.js');
var antd = require('antd');
var cs = require('classnames');
var React = require('react');
var index$4 = require('../../coreX/OverflowTooltip/index.js');
var index$1 = require('../../coreX/Show/index.js');
var SmallDialog_style = require('./SmallDialog.style.js');
var SmallDialog_widget = require('./SmallDialog.widget.js');
var index$3 = require('../Icon/index.js');
var index$5 = require('../Button/index.js');
var antd5 = require('antd5');
var index$2 = require('../Typo/index.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 __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;
};
const DefaultTitleRender = ({
  title
}) => {
  return /* @__PURE__ */ React__default.default.createElement("span", { className: cs__default.default(index$2.Typo.Display.d2_bold_title) }, title);
};
const SmallDialog = ({
  width = 492,
  title,
  TitleRender,
  cancelText,
  okText,
  showOk = true,
  onOk,
  onCancel,
  maskClosable = true,
  closable = true,
  className,
  footerClassName,
  okButtonProps,
  cancelButtonProps,
  children,
  hideFooter = false,
  showFooterErrorIcon = true,
  error,
  confirmLoading,
  initializing,
  initializingError,
  initializingSkeletonRows,
  "data-testid": dataTestId
}) => {
  const { t } = useParrotTranslation();
  const popModal = index.usePopModal();
  const sub = (suffix) => dataTestId ? `${dataTestId}-${suffix}` : void 0;
  const handleClose = () => {
    if (onCancel) {
      onCancel(popModal);
    } else {
      popModal();
    }
  };
  const handleOk = () => {
    if (onOk) {
      onOk(popModal);
    } else {
      popModal();
    }
  };
  const CustomTitleRender = TitleRender || DefaultTitleRender;
  const defaultTitle = initializingError ? t("common.load_failed") : "";
  const defaultCancelText = showOk ? t("common.cancel") : t("common.close");
  const defaultOkText = initializingError ? t("common.retry") : t("common.confirm");
  return /* @__PURE__ */ React__default.default.createElement(
    antd.Modal,
    {
      visible: true,
      "data-testid": dataTestId,
      width,
      title: initializing ? /* @__PURE__ */ React__default.default.createElement(SmallDialog_widget.ModelTitleSkeleton, null) : /* @__PURE__ */ React__default.default.createElement(CustomTitleRender, { title: title || defaultTitle }),
      footer: initializing ? null : hideFooter ? /* @__PURE__ */ React__default.default.createElement("div", null) : /* @__PURE__ */ React__default.default.createElement(
        "div",
        {
          className: cs__default.default(SmallDialog_style.FooterStyle, footerClassName, {
            "has-error": !!error
          })
        },
        /* @__PURE__ */ React__default.default.createElement(index$1.Show, { condition: !!error }, /* @__PURE__ */ React__default.default.createElement(
          antd5.Flex,
          {
            gap: 4,
            align: "center",
            className: core.cx(SmallDialog_style.ErrorTextStyle, index$2.Typo.Label.l3_regular)
          },
          /* @__PURE__ */ React__default.default.createElement(index$1.Show, { condition: showFooterErrorIcon }, /* @__PURE__ */ React__default.default.createElement(
            index$3,
            {
              src: iconsReact.ExclamationErrorCircleFill16RedIcon,
              iconHeight: 16,
              iconWidth: 16
            }
          )),
          /* @__PURE__ */ React__default.default.createElement(
            index$4,
            {
              multiLines: 3,
              content: error,
              tooltip: error
            }
          )
        )),
        /* @__PURE__ */ React__default.default.createElement(antd5.Flex, null, /* @__PURE__ */ React__default.default.createElement(
          index$5,
          __spreadValues({
            type: showOk ? "quiet" : "ordinary",
            onClick: handleClose,
            className: index$2.Typo.Label.l2_bold_title,
            "data-testid": sub("cancel")
          }, cancelButtonProps),
          cancelText || defaultCancelText
        ), showOk && /* @__PURE__ */ React__default.default.createElement(
          index$5,
          __spreadValues({
            type: "primary",
            onClick: handleOk,
            loading: confirmLoading,
            "data-testid": sub("ok")
          }, okButtonProps),
          okText || defaultOkText
        ))
      ),
      onCancel: handleClose,
      closable,
      maskClosable,
      className: cs__default.default(SmallDialog_style.DialogStyle, className, {
        "initializing-error": initializingError,
        "no-footer": hideFooter
      }),
      closeIcon: /* @__PURE__ */ React__default.default.createElement(
        index$3,
        {
          src: iconsReact.XmarkCloseBold24TertiaryIcon,
          className: SmallDialog_style.CloseIconStyle,
          iconHeight: 24,
          iconWidth: 24,
          hoverSrc: iconsReact.XmarkCloseBold24SecondaryIcon,
          "data-testid": sub("close")
        }
      )
    },
    initializing ? /* @__PURE__ */ React__default.default.createElement(SmallDialog_widget.ModelContentSkeleton, { num: initializingSkeletonRows }) : initializingError ? /* @__PURE__ */ React__default.default.createElement(SmallDialog_widget.ModelInitializingError, { error: initializingError }) : children
  );
};

exports.SmallDialog = SmallDialog;
