import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import { useCallback, cloneElement } from 'react';
import { HStack, ButtonGroup, Stack, Box } from '@chakra-ui/react';
import 'chakra-react-select';
import 'react-icons/fa';
import '../../styles/theme.js';
import 'react-icons/bs';
import 'react-router-dom';
import Heading from '../Heading/Heading.js';
import 'react-icons/md';
import useConfirmModal from './useConfirmModal.js';
import { WarningIcon, CheckCircleIcon } from '@chakra-ui/icons';
import { noop } from '../../utils/utils.js';
import Button from '../Button/Button.js';
import '../Form/FormProvider.js';
import 'react-hook-form';
import '@hookform/devtools';
import '@hookform/resolvers/yup';
import 'yup';
import '../Inputs/Slider/chakra/ChakraTooltip.js';
import 'react-icons/io';
import '../CodeBlock/CodeBlock.js';
import '../DataTable/DataTable.js';
import '../../styles/colors.js';
import 'date-fns/locale/en-US';
import 'date-fns-tz/zonedTimeToUtc';
import 'react-datepicker';
import 'date-fns';
import 'date-fns-tz';
import '../Inputs/DateTimeInput/Calendar.js';
import '../Popover/PopoverContent.js';
import '../Popover/Popover.js';
import '../Badge/Badge.js';
import 'react-icons/io5';
import '@textea/json-viewer';
import 'react-markdown';
import 'remark-gfm';
import 'remark-emoji';

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: noop
};
const ConfirmModal = (props) => {
  const {
    onConfirm,
    onClose = DEFAULT_VALUES.NOOP,
    trigger,
    primaryActionLabel,
    secondaryActionLabel,
    heading,
    variant = DEFAULT_VALUES.VARIANT,
    autoCloseOnConfirm = true,
    children
  } = props;
  const handleTriggerOnClick = useCallback(() => __async(void 0, null, function* () {
    yield showModal().catch(() => {
      onCancel();
    });
  }), []);
  const onCancel = useCallback(() => {
    onClose();
  }, [onClose]);
  const onConfirmClick = useCallback(
    (modalConfirm) => {
      if (autoCloseOnConfirm) {
        modalConfirm();
      }
      onConfirm(modalConfirm);
    },
    [onConfirm]
  );
  const getHeadingElement = useCallback(() => {
    switch (variant) {
      case "success":
        return /* @__PURE__ */ jsxs(HStack, { spacing: "0.625rem", children: [
          /* @__PURE__ */ jsx(CheckCircleIcon, { color: "green.500" }),
          /* @__PURE__ */ jsx(Heading, { variant: "md", children: heading })
        ] });
      case "error":
        return /* @__PURE__ */ jsxs(HStack, { spacing: "0.625rem", children: [
          /* @__PURE__ */ jsx(WarningIcon, { color: "red.500" }),
          /* @__PURE__ */ jsx(Heading, { variant: "md", children: heading })
        ] });
      default:
        return /* @__PURE__ */ jsx(Heading, { variant: "md", children: heading });
    }
  }, [variant, heading]);
  const getPrimaryButtonLabel = 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 = 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 = useCallback(
    (confirm, cancel) => {
      switch (variant) {
        case "default":
          return /* @__PURE__ */ jsxs(ButtonGroup, { children: [
            /* @__PURE__ */ jsx(Button, { variant: "ghost", onClick: cancel, children: getSecondaryButtonLabel() }),
            /* @__PURE__ */ jsx(Button, { variant: "solid", onClick: () => onConfirmClick(confirm), children: getPrimaryButtonLabel() })
          ] });
        case "delete":
          return /* @__PURE__ */ jsxs(ButtonGroup, { children: [
            /* @__PURE__ */ jsx(Button, { variant: "delete", onClick: () => onConfirmClick(confirm), children: getPrimaryButtonLabel() }),
            /* @__PURE__ */ jsx(Button, { variant: "ghost", onClick: cancel, children: getSecondaryButtonLabel() })
          ] });
        case "success":
        case "error":
          return /* @__PURE__ */ jsx(ButtonGroup, { children: /* @__PURE__ */ jsx(Button, { variant: "solid", onClick: () => onConfirmClick(confirm), children: getPrimaryButtonLabel() }) });
      }
    },
    [variant, getPrimaryButtonLabel, getSecondaryButtonLabel, onConfirmClick]
  );
  const getTriggerElement = useCallback(() => {
    if (typeof trigger === "string") {
      return /* @__PURE__ */ jsx(Button, { variant: "solid", onClick: handleTriggerOnClick, children: trigger });
    } else {
      return 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__ */ jsxs(Stack, { spacing: "4", children: [
        /* @__PURE__ */ jsx(Box, { children }),
        /* @__PURE__ */ jsx(Box, { alignSelf: "end", children: getButtonElements(confirm, cancel) })
      ] });
    }
  });
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    getTriggerElement(),
    renderModal()
  ] });
};

export { ConfirmModal as default };
//# sourceMappingURL=ConfirmModal.js.map
