'use strict';

var React = require('react');
var index$1 = require('../../core/Modal/index.js');
var iconsReact = require('@cloudtower/icons-react');
var useParrotTranslation = require('../../hooks/useParrotTranslation.js');
var core = require('@linaria/core');
var utils = require('./utils.js');
var KubeConfigModal_style = require('./KubeConfigModal.style.js');
var index = require('../../core/KitStoreProvider/index.js');
var message = require('../../core/message/message.js');
var index$2 = require('../../core/Button/index.js');
var index$3 = require('../../core/Icon/index.js');
var index$5 = require('../../core/Loading/index.js');
var index$4 = require('../../core/Typo/index.js');

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

var React__default = /*#__PURE__*/_interopDefault(React);

const ErrorContent = (props) => {
  const { refetch, errorText } = props;
  const { t } = useParrotTranslation();
  return /* @__PURE__ */ React__default.default.createElement(KubeConfigModal_style.ErrorWrapper, { className: props.className, style: props.style }, /* @__PURE__ */ React__default.default.createElement(KubeConfigModal_style.ErrorContentWrapper, null, /* @__PURE__ */ React__default.default.createElement("p", { className: core.cx(index$4.Typo.Label.l1_regular_title, "title") }, errorText || t("common.obtain_data_error")), !refetch ? null : /* @__PURE__ */ React__default.default.createElement(
    index$2,
    {
      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 = index.usePopModal();
  const onCopy = React.useCallback(() => {
    utils.copy(data);
    message.default.success(t("common.copy_kubeconfig_success"));
  }, [data, t]);
  const onDownload = React.useCallback(() => {
    utils.download(`${downloadName}.yaml`, data);
    popModal();
  }, [data, popModal, downloadName]);
  return /* @__PURE__ */ React__default.default.createElement(
    index$1,
    {
      visible: true,
      fullscreen: true,
      className: KubeConfigModal_style.KubeConfigModalStyle,
      onCancel: () => {
        popModal();
      },
      footer: /* @__PURE__ */ React__default.default.createElement("div", { className: KubeConfigModal_style.KubeConfigFooterStyle }, /* @__PURE__ */ React__default.default.createElement(
        index$2,
        {
          type: "default",
          prefixIcon: /* @__PURE__ */ React__default.default.createElement(
            index$3,
            {
              src: iconsReact.ClipboardCopy16GradientGrayIcon,
              hoverSrc: iconsReact.ClipboardCopy16GradientBlueIcon
            }
          ),
          onClick: onCopy
        },
        t("common.copy")
      ), /* @__PURE__ */ React__default.default.createElement("div", null, /* @__PURE__ */ React__default.default.createElement(
        index$2,
        {
          type: "text",
          onClick: () => {
            popModal();
          }
        },
        t("common.cancel")
      ), /* @__PURE__ */ React__default.default.createElement(
        index$2,
        {
          type: "primary",
          onClick: onDownload,
          disabled: loading || !!error
        },
        t("common.download")
      )))
    },
    /* @__PURE__ */ React__default.default.createElement("div", { className: KubeConfigModal_style.KubeConfigWrapperStyle }, /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx(KubeConfigModal_style.KubeConfigTitleStyle, index$4.Typo.Display.d1s_bold_title) }, t("common.download_kubeconfig")), /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx(KubeConfigModal_style.KubeConfigTipStyle, index$4.Typo.Label.l1_regular) }, customDesc || t("common.download_kubeconfig_tip")), loading ? /* @__PURE__ */ React__default.default.createElement(index$5, { fullView: true }) : error ? /* @__PURE__ */ React__default.default.createElement("div", { className: KubeConfigModal_style.KubeConfigContentStyle }, /* @__PURE__ */ React__default.default.createElement(ErrorContent, { refetch, errorText: error })) : /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, /* @__PURE__ */ React__default.default.createElement("div", { className: KubeConfigModal_style.KubeConfigContentStyle }, data)))
  );
};
var KubeConfigModal$1 = KubeConfigModal;

module.exports = KubeConfigModal$1;
