import { ClipboardCopy16GradientGrayIcon, ClipboardCopy16GradientBlueIcon } from '@cloudtower/icons-react';
import { cx } from '@linaria/core';
import LegacyModal from '../../core/LegacyModal/index.js';
import useParrotTranslation from '../../hooks/useParrotTranslation.js';
import React__default, { useCallback } from 'react';
import { KubeConfigModalStyle, KubeConfigFooterStyle, KubeConfigWrapperStyle, KubeConfigTitleStyle, KubeConfigTipStyle, KubeConfigContentStyle, ErrorWrapper, ErrorContentWrapper } from './KubeConfigModal.style.js';
import { copy, download } from './utils.js';
import { usePopModal } from '../../core/KitStoreProvider/index.js';
import _message from '../../core/message/message.js';
import Button from '../../core/Button/index.js';
import Icon from '../../core/Icon/index.js';
import Loading from '../../core/Loading/index.js';
import { Typo } from '../../core/Typo/index.js';

const ErrorContent = (props) => {
  const { refetch, errorText } = props;
  const { t } = useParrotTranslation();
  return /* @__PURE__ */ React__default.createElement(ErrorWrapper, { className: props.className, style: props.style }, /* @__PURE__ */ React__default.createElement(ErrorContentWrapper, null, /* @__PURE__ */ React__default.createElement("p", { className: cx(Typo.Label.l1_regular_title, "title") }, errorText || t("common.obtain_data_error")), !refetch ? null : /* @__PURE__ */ React__default.createElement(
    Button,
    {
      size: "small",
      type: "ordinary",
      onClick: (e) => {
        e.stopPropagation();
        refetch == null ? void 0 : refetch();
      }
    },
    t("common.retry")
  )));
};
const KubeConfigModal = ({
  downloadName,
  customDesc,
  loading,
  data,
  error,
  refetch
}) => {
  const { t } = useParrotTranslation();
  const popModal = usePopModal();
  const onCopy = useCallback(() => {
    copy(data);
    _message.success(t("common.copy_kubeconfig_success"));
  }, [data, t]);
  const onDownload = useCallback(() => {
    download(`${downloadName}.yaml`, data);
    popModal();
  }, [data, popModal, downloadName]);
  return /* @__PURE__ */ React__default.createElement(
    LegacyModal,
    {
      visible: true,
      fullscreen: true,
      className: KubeConfigModalStyle,
      onCancel: () => {
        popModal();
      },
      footer: /* @__PURE__ */ React__default.createElement("div", { className: KubeConfigFooterStyle }, /* @__PURE__ */ React__default.createElement(
        Button,
        {
          type: "default",
          prefixIcon: /* @__PURE__ */ React__default.createElement(
            Icon,
            {
              src: ClipboardCopy16GradientGrayIcon,
              hoverSrc: ClipboardCopy16GradientBlueIcon
            }
          ),
          onClick: onCopy
        },
        t("common.copy")
      ), /* @__PURE__ */ React__default.createElement("div", null, /* @__PURE__ */ React__default.createElement(
        Button,
        {
          type: "text",
          onClick: () => {
            popModal();
          }
        },
        t("common.cancel")
      ), /* @__PURE__ */ React__default.createElement(
        Button,
        {
          type: "primary",
          onClick: onDownload,
          disabled: loading || !!error
        },
        t("common.download")
      )))
    },
    /* @__PURE__ */ React__default.createElement("div", { className: KubeConfigWrapperStyle }, /* @__PURE__ */ React__default.createElement("div", { className: cx(KubeConfigTitleStyle, Typo.Display.d1s_bold_title) }, t("common.download_kubeconfig")), /* @__PURE__ */ React__default.createElement("div", { className: cx(KubeConfigTipStyle, Typo.Label.l1_regular) }, customDesc || t("common.download_kubeconfig_tip")), loading ? /* @__PURE__ */ React__default.createElement(Loading, { fullView: true }) : error ? /* @__PURE__ */ React__default.createElement("div", { className: KubeConfigContentStyle }, /* @__PURE__ */ React__default.createElement(ErrorContent, { refetch, errorText: error })) : /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, /* @__PURE__ */ React__default.createElement("div", { className: KubeConfigContentStyle }, data)))
  );
};
var KubeConfigModal$1 = KubeConfigModal;

export { KubeConfigModal$1 as default };
