import { ExclamationErrorCircleFill16RedIcon, XmarkCloseBold24TertiaryIcon, XmarkCloseBold24SecondaryIcon } from '@cloudtower/icons-react';
import { cx } from '@linaria/core';
import { usePopModal } from '../KitStoreProvider/index.js';
import useParrotTranslation from '../../hooks/useParrotTranslation.js';
import { Modal } from 'antd';
import cs from 'classnames';
import React__default from 'react';
import OverflowTooltip from '../../coreX/OverflowTooltip/index.js';
import { Show } from '../../coreX/Show/index.js';
import { FooterStyle, ErrorTextStyle, DialogStyle, CloseIconStyle } from './SmallDialog.style.js';
import { ModelTitleSkeleton, ModelContentSkeleton, ModelInitializingError } from './SmallDialog.widget.js';
import Icon from '../Icon/index.js';
import Button from '../Button/index.js';
import { Flex } from 'antd5';
import { Typo } from '../Typo/index.js';

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.createElement("span", { className: cs(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
}) => {
  const { t } = useParrotTranslation();
  const popModal = usePopModal();
  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.createElement(
    Modal,
    {
      visible: true,
      width,
      title: initializing ? /* @__PURE__ */ React__default.createElement(ModelTitleSkeleton, null) : /* @__PURE__ */ React__default.createElement(CustomTitleRender, { title: title || defaultTitle }),
      footer: initializing ? null : hideFooter ? /* @__PURE__ */ React__default.createElement("div", null) : /* @__PURE__ */ React__default.createElement(
        "div",
        {
          className: cs(FooterStyle, footerClassName, {
            "has-error": !!error
          })
        },
        /* @__PURE__ */ React__default.createElement(Show, { condition: !!error }, /* @__PURE__ */ React__default.createElement(
          Flex,
          {
            gap: 4,
            align: "center",
            className: cx(ErrorTextStyle, Typo.Label.l3_regular)
          },
          /* @__PURE__ */ React__default.createElement(Show, { condition: showFooterErrorIcon }, /* @__PURE__ */ React__default.createElement(
            Icon,
            {
              src: ExclamationErrorCircleFill16RedIcon,
              iconHeight: 16,
              iconWidth: 16
            }
          )),
          /* @__PURE__ */ React__default.createElement(
            OverflowTooltip,
            {
              multiLines: 3,
              content: error,
              tooltip: error
            }
          )
        )),
        /* @__PURE__ */ React__default.createElement(Flex, null, /* @__PURE__ */ React__default.createElement(
          Button,
          __spreadValues({
            type: showOk ? "quiet" : "ordinary",
            onClick: handleClose,
            className: Typo.Label.l2_bold_title
          }, cancelButtonProps),
          cancelText || defaultCancelText
        ), showOk && /* @__PURE__ */ React__default.createElement(
          Button,
          __spreadValues({
            type: "primary",
            onClick: handleOk,
            loading: confirmLoading
          }, okButtonProps),
          okText || defaultOkText
        ))
      ),
      onCancel: handleClose,
      closable,
      maskClosable,
      className: cs(DialogStyle, className, {
        "initializing-error": initializingError,
        "no-footer": hideFooter
      }),
      closeIcon: /* @__PURE__ */ React__default.createElement(
        Icon,
        {
          src: XmarkCloseBold24TertiaryIcon,
          className: CloseIconStyle,
          iconHeight: 24,
          iconWidth: 24,
          hoverSrc: XmarkCloseBold24SecondaryIcon
        }
      )
    },
    initializing ? /* @__PURE__ */ React__default.createElement(ModelContentSkeleton, { num: initializingSkeletonRows }) : initializingError ? /* @__PURE__ */ React__default.createElement(ModelInitializingError, { error: initializingError }) : children
  );
};

export { SmallDialog };
