'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var react = require('@chakra-ui/react');
require('chakra-react-select');
require('react-icons/fa');
require('../../styles/theme.cjs');
require('react-icons/bs');
require('react-router-dom');
var Heading = require('../Heading/Heading.cjs');
require('react-icons/md');
var useConfirmModal = require('./useConfirmModal.cjs');
var icons = require('@chakra-ui/icons');
var utils = require('../../utils/utils.cjs');
var Button = require('../Button/Button.cjs');
require('../Form/FormProvider.cjs');
require('react-hook-form');
require('@hookform/devtools');
require('@hookform/resolvers/yup');
require('yup');
require('../Inputs/Slider/chakra/ChakraTooltip.cjs');
require('react-icons/io');
require('react-syntax-highlighter');
require('../DataTable/DataTable.cjs');
require('../../styles/colors.cjs');
require('date-fns/locale/en-US');
require('date-fns-tz/zonedTimeToUtc');
require('react-datepicker');
require('date-fns');
require('date-fns-tz');
require('../Inputs/DateTimeInput/Calendar.cjs');
require('../Popover/PopoverContent.cjs');
require('../Popover/Popover.cjs');
require('../Badge/Badge.cjs');
require('react-icons/io5');
require('@textea/json-viewer');
require('react-markdown');
require('remark-gfm');
require('remark-emoji');
require('../Login/auth0/Auth0ModalLink.cjs');

var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
const DEFAULT_VALUES = {
  LABEL_CONFIRM: "Confirm",
  LABEL_DELETE: "Delete",
  LABEL_CLOSE: "Close",
  LABEL_CANCEL: "Cancel",
  VARIANT: "default",
  NOOP: utils.noop
};
const ConfirmModal = (props) => {
  const {
    onConfirm,
    onClose = DEFAULT_VALUES.NOOP,
    trigger,
    primaryActionLabel,
    secondaryActionLabel,
    heading,
    variant = DEFAULT_VALUES.VARIANT,
    autoCloseOnConfirm = true,
    children
  } = props;
  const handleTriggerOnClick = React.useCallback(() => __async(void 0, null, function* () {
    yield showModal().catch(() => {
      onCancel();
    });
  }), []);
  const onCancel = React.useCallback(() => {
    onClose();
  }, [onClose]);
  const onConfirmClick = React.useCallback(
    (modalConfirm) => {
      if (autoCloseOnConfirm) {
        modalConfirm();
      }
      onConfirm(modalConfirm);
    },
    [onConfirm]
  );
  const getHeadingElement = React.useCallback(() => {
    switch (variant) {
      case "success":
        return /* @__PURE__ */ jsxRuntime.jsxs(react.HStack, { spacing: "0.625rem", children: [
          /* @__PURE__ */ jsxRuntime.jsx(icons.CheckCircleIcon, { color: "green.500" }),
          /* @__PURE__ */ jsxRuntime.jsx(Heading, { variant: "md", children: heading })
        ] });
      case "error":
        return /* @__PURE__ */ jsxRuntime.jsxs(react.HStack, { spacing: "0.625rem", children: [
          /* @__PURE__ */ jsxRuntime.jsx(icons.WarningIcon, { color: "red.500" }),
          /* @__PURE__ */ jsxRuntime.jsx(Heading, { variant: "md", children: heading })
        ] });
      default:
        return /* @__PURE__ */ jsxRuntime.jsx(Heading, { variant: "md", children: heading });
    }
  }, [variant, heading]);
  const getPrimaryButtonLabel = React.useCallback(() => {
    if (primaryActionLabel) {
      return primaryActionLabel;
    } else {
      switch (variant) {
        case "delete":
          return DEFAULT_VALUES.LABEL_DELETE;
        case "success":
        case "error":
          return DEFAULT_VALUES.LABEL_CLOSE;
        default:
          return DEFAULT_VALUES.LABEL_CONFIRM;
      }
    }
  }, [variant, primaryActionLabel]);
  const getSecondaryButtonLabel = React.useCallback(() => {
    if (secondaryActionLabel) {
      return secondaryActionLabel;
    } else {
      switch (variant) {
        case "success":
        case "error":
          return DEFAULT_VALUES.LABEL_CLOSE;
        case "delete":
        default:
          return DEFAULT_VALUES.LABEL_CANCEL;
      }
    }
  }, [variant, secondaryActionLabel]);
  const getButtonElements = React.useCallback(
    (confirm, cancel) => {
      switch (variant) {
        case "default":
          return /* @__PURE__ */ jsxRuntime.jsxs(react.ButtonGroup, { children: [
            /* @__PURE__ */ jsxRuntime.jsx(Button, { variant: "ghost", onClick: cancel, children: getSecondaryButtonLabel() }),
            /* @__PURE__ */ jsxRuntime.jsx(Button, { variant: "solid", onClick: () => onConfirmClick(confirm), children: getPrimaryButtonLabel() })
          ] });
        case "delete":
          return /* @__PURE__ */ jsxRuntime.jsxs(react.ButtonGroup, { children: [
            /* @__PURE__ */ jsxRuntime.jsx(Button, { variant: "delete", onClick: () => onConfirmClick(confirm), children: getPrimaryButtonLabel() }),
            /* @__PURE__ */ jsxRuntime.jsx(Button, { variant: "ghost", onClick: cancel, children: getSecondaryButtonLabel() })
          ] });
        case "success":
        case "error":
          return /* @__PURE__ */ jsxRuntime.jsx(react.ButtonGroup, { children: /* @__PURE__ */ jsxRuntime.jsx(Button, { variant: "solid", onClick: () => onConfirmClick(confirm), children: getPrimaryButtonLabel() }) });
      }
    },
    [variant, getPrimaryButtonLabel, getSecondaryButtonLabel, onConfirmClick]
  );
  const getTriggerElement = React.useCallback(() => {
    if (typeof trigger === "string") {
      return /* @__PURE__ */ jsxRuntime.jsx(Button, { variant: "solid", onClick: handleTriggerOnClick, children: trigger });
    } else {
      return React.cloneElement(trigger, {
        onClick: (event) => {
          if (trigger.props.onClick) {
            trigger.props.onClick(event);
          }
          handleTriggerOnClick();
        }
      });
    }
  }, [handleTriggerOnClick, trigger]);
  const { renderModal, showModal } = useConfirmModal({
    heading: getHeadingElement(),
    render: ({ confirm, cancel }) => {
      return /* @__PURE__ */ jsxRuntime.jsxs(react.Stack, { spacing: "4", children: [
        /* @__PURE__ */ jsxRuntime.jsx(react.Box, { children }),
        /* @__PURE__ */ jsxRuntime.jsx(react.Box, { alignSelf: "end", children: getButtonElements(confirm, cancel) })
      ] });
    }
  });
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    getTriggerElement(),
    renderModal()
  ] });
};

module.exports = ConfirmModal;
//# sourceMappingURL=ConfirmModal.cjs.map
