import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__chakra_ui_react__ from "@chakra-ui/react";
import * as __WEBPACK_EXTERNAL_MODULE__useConfirmModal_mjs__ from "./useConfirmModal.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__chakra_ui_icons__ from "@chakra-ui/icons";
import * as __WEBPACK_EXTERNAL_MODULE__utils_utils_mjs__ from "../../utils/utils.mjs";
// default prop values
const DEFAULT_VALUES = {
    LABEL_CONFIRM: 'Confirm',
    LABEL_DELETE: 'Delete',
    LABEL_CLOSE: 'Close',
    LABEL_CANCEL: 'Cancel',
    VARIANT: 'default',
    NOOP: __WEBPACK_EXTERNAL_MODULE__utils_utils_mjs__.noop
};
const ConfirmModal = (props)=>{
    const { onConfirm, onClose = DEFAULT_VALUES.NOOP, trigger, primaryActionLabel, secondaryActionLabel, heading, variant = DEFAULT_VALUES.VARIANT, autoCloseOnConfirm = true, children } = props;
    const handleTriggerOnClick = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(async ()=>{
        await showModal().catch(()=>{
            onCancel();
        });
    }, []);
    const onCancel = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        onClose();
    }, [
        onClose
    ]);
    const onConfirmClick = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((modalConfirm)=>{
        if (autoCloseOnConfirm) modalConfirm();
        // call back to user
        onConfirm(modalConfirm);
    }, [
        onConfirm
    ]);
    const getHeadingElement = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        switch(variant){
            case 'success':
                return /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.HStack, {
                    spacing: "0.625rem"
                }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_icons__.CheckCircleIcon, {
                    color: 'green.500'
                }), /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Heading, {
                    variant: "md"
                }, heading));
            case 'error':
                return /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.HStack, {
                    spacing: "0.625rem"
                }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_icons__.WarningIcon, {
                    color: 'red.500'
                }), /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Heading, {
                    variant: "md"
                }, heading));
            default:
                return /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Heading, {
                    variant: "md"
                }, heading);
        }
    }, [
        variant,
        heading
    ]);
    const getPrimaryButtonLabel = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        if (primaryActionLabel) return primaryActionLabel;
        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 = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        if (secondaryActionLabel) return secondaryActionLabel;
        switch(variant){
            case 'success':
            case 'error':
                return DEFAULT_VALUES.LABEL_CLOSE;
            case 'delete':
            default:
                return DEFAULT_VALUES.LABEL_CANCEL;
        }
    }, [
        variant,
        secondaryActionLabel
    ]);
    const getButtonElements = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((confirm, cancel)=>{
        switch(variant){
            case 'default':
                return /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.ButtonGroup, null, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Button, {
                    variant: "ghost",
                    onClick: cancel
                }, getSecondaryButtonLabel()), /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Button, {
                    variant: "solid",
                    onClick: ()=>onConfirmClick(confirm)
                }, getPrimaryButtonLabel()));
            case 'delete':
                return /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.ButtonGroup, null, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Button, {
                    variant: "delete",
                    onClick: ()=>onConfirmClick(confirm)
                }, getPrimaryButtonLabel()), /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Button, {
                    variant: "ghost",
                    onClick: cancel
                }, getSecondaryButtonLabel()));
            case 'success':
            case 'error':
                return /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.ButtonGroup, null, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Button, {
                    variant: "solid",
                    onClick: ()=>onConfirmClick(confirm)
                }, getPrimaryButtonLabel()));
        }
    }, [
        variant,
        getPrimaryButtonLabel,
        getSecondaryButtonLabel,
        onConfirmClick
    ]);
    // allows for both custom components or simple text as the trigger;
    // if custom component is passed as the delete label, we
    // clone the element and wrap the onClick.
    const getTriggerElement = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        if ('string' == typeof trigger) return /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Button, {
            variant: "solid",
            onClick: handleTriggerOnClick
        }, trigger);
        // override the onClick to open the modal in addition to performing
        // the onClick of the label component.
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.cloneElement)(trigger, {
            onClick: (event)=>{
                if (trigger.props.onClick) trigger.props.onClick(event);
                handleTriggerOnClick();
            }
        });
    }, [
        handleTriggerOnClick,
        trigger
    ]);
    const { renderModal, showModal } = (0, __WEBPACK_EXTERNAL_MODULE__useConfirmModal_mjs__["default"])({
        heading: getHeadingElement(),
        render: ({ confirm, cancel })=>/*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Stack, {
                spacing: "4"
            }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Box, null, children), /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Box, {
                alignSelf: "end"
            }, getButtonElements(confirm, cancel)))
    });
    return /*#__PURE__*/ React.createElement(React.Fragment, null, getTriggerElement(), renderModal());
};
/* ESM default export */ const ConfirmModal_rslib_entry_ = ConfirmModal;
export { ConfirmModal_rslib_entry_ as default };
