'use strict';

var iconsReact = require('@cloudtower/icons-react');
var core = require('@linaria/core');
var index$3 = require('../Button/index.js');
var index$4 = require('../Icon/index.js');
var index = require('../KitStoreProvider/index.js');
var index$1 = require('../Typo/index.js');
var index$2 = require('../../coreX/OverflowTooltip/index.js');
var useParrotTranslation = require('../../hooks/useParrotTranslation.js');
var antd = require('antd');
var cs = require('classnames');
var React = require('react');
var SmallDialog_widget = require('../SmallDialog/SmallDialog.widget.js');
var styles = require('./styles.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;
};
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;
};
function ImmersiveDialog(props) {
  const _a = props, {
    className,
    title,
    children,
    footerLeftAction,
    left,
    right,
    leftClassName,
    rightClassName,
    closeIcon,
    footer,
    showCancel = true,
    showOk = true,
    confirmLoading,
    okButtonProps,
    cancelButtonProps,
    okText,
    cancelText,
    error,
    hideFooter = false,
    showFooterErrorIcon = true,
    visible = true,
    isContentFull,
    onCancel,
    onOk,
    initializing,
    initializingError,
    "data-testid": dataTestId
  } = _a, restProps = __objRest(_a, [
    "className",
    "title",
    "children",
    "footerLeftAction",
    "left",
    "right",
    "leftClassName",
    "rightClassName",
    "closeIcon",
    "footer",
    "showCancel",
    "showOk",
    "confirmLoading",
    "okButtonProps",
    "cancelButtonProps",
    "okText",
    "cancelText",
    "error",
    "hideFooter",
    "showFooterErrorIcon",
    "visible",
    "isContentFull",
    "onCancel",
    "onOk",
    "initializing",
    "initializingError",
    "data-testid"
  ]);
  const { t } = useParrotTranslation();
  const popModal = index.usePopModal();
  const sub = React.useCallback(
    (suffix) => dataTestId ? `${dataTestId}-${suffix}` : void 0,
    [dataTestId]
  );
  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");
  const _cancelText = React.useMemo(() => {
    let text = cancelText || defaultCancelText;
    return text;
  }, [cancelText, defaultCancelText]);
  const _confirmText = React.useMemo(() => {
    let text = okText || defaultOkText;
    return text;
  }, [okText, defaultOkText]);
  const finalFooter = React.useMemo(() => {
    if (initializing) {
      return null;
    }
    if (hideFooter)
      return /* @__PURE__ */ React__default.default.createElement("div", null);
    return footer || /* @__PURE__ */ React__default.default.createElement("div", { className: styles.FooterStyle }, /* @__PURE__ */ React__default.default.createElement("div", { className: styles.FooterButtonStyle }, footerLeftAction, error && /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement("span", { className: core.cx(styles.ErrorStyle, index$1.Typo.Label.l2_regular) }, showFooterErrorIcon && /* @__PURE__ */ React__default.default.createElement(
      iconsReact.ExclamationErrorCircleFill16RedIcon,
      {
        className: styles.ErrorIconStyle
      }
    ), /* @__PURE__ */ React__default.default.createElement(
      index$2,
      {
        className: styles.ErrorTextStyle,
        multiLines: 3,
        content: error,
        tooltip: error
      }
    )))), /* @__PURE__ */ React__default.default.createElement("div", { className: styles.FooterButtonRightStyle }, showCancel ? /* @__PURE__ */ React__default.default.createElement(
      index$3,
      __spreadValues({
        size: "large",
        type: "quiet",
        className: styles.CancelButtonStyle,
        "data-testid": sub("cancel"),
        onMouseDown: (e) => {
          e.preventDefault();
        },
        onClick: (e) => {
          popModal();
          onCancel == null ? void 0 : onCancel(e);
        }
      }, cancelButtonProps),
      /* @__PURE__ */ React__default.default.createElement("span", { className: index$1.Typo.Label.l1_bold_title }, _cancelText)
    ) : null, showOk ? /* @__PURE__ */ React__default.default.createElement(
      index$3,
      __spreadValues({
        size: "large",
        onClick: (e) => {
          onOk == null ? void 0 : onOk(e);
        },
        type: "primary",
        loading: confirmLoading,
        "data-testid": sub("ok")
      }, okButtonProps),
      _confirmText
    ) : null));
  }, [
    initializing,
    hideFooter,
    footer,
    footerLeftAction,
    error,
    showFooterErrorIcon,
    showCancel,
    sub,
    cancelButtonProps,
    _cancelText,
    showOk,
    confirmLoading,
    okButtonProps,
    _confirmText,
    popModal,
    onCancel,
    onOk
  ]);
  return /* @__PURE__ */ React__default.default.createElement(
    antd.Modal,
    __spreadValues({
      "data-testid": dataTestId,
      width: "calc(100% - 20px)",
      title: initializing ? /* @__PURE__ */ React__default.default.createElement(SmallDialog_widget.ModelTitleSkeleton, null) : /* @__PURE__ */ React__default.default.createElement("span", { className: core.cx(index$1.Typo.Display.d1_bold_title, styles.TitleStyle) }, title || defaultTitle),
      className: cs__default.default(
        styles.ImmersiveDialogStyle,
        isContentFull ? styles.FullContentStyle : styles.MultiAreaStyle,
        className,
        {
          "initializing-error": initializingError,
          "no-footer": hideFooter
        }
      ),
      closeIcon: closeIcon != null ? closeIcon : /* @__PURE__ */ React__default.default.createElement(
        index$4,
        {
          src: iconsReact.XmarkCloseBold24TertiaryIcon,
          className: styles.CloseIconStyle,
          iconHeight: 24,
          iconWidth: 24,
          hoverSrc: iconsReact.XmarkCloseBold24SecondaryIcon,
          "data-testid": sub("close")
        }
      ),
      footer: finalFooter,
      onCancel: (e) => {
        popModal();
        onCancel == null ? void 0 : onCancel(e);
      },
      visible,
      destroyOnClose: true
    }, restProps),
    /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx(styles.BodyStyle, isContentFull ? "" : styles.MultiAreaBodyStyle) }, initializing || initializingError ? /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement("div", { className: "middle" }, initializing ? /* @__PURE__ */ React__default.default.createElement(SmallDialog_widget.ModelContentSkeleton, { num: 4 }) : /* @__PURE__ */ React__default.default.createElement(SmallDialog_widget.ModelInitializingError, { error: initializingError }))) : /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, isContentFull ? null : /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("left", leftClassName) }, left), /* @__PURE__ */ React__default.default.createElement("div", { className: "middle" }, children), isContentFull ? null : /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("right", rightClassName) }, right)))
  );
}

exports.ImmersiveDialog = ImmersiveDialog;
