"use strict";
// The require scope
var __webpack_require__ = {};
/************************************************************************/ // webpack/runtime/compat_get_default_export
(()=>{
    // getDefaultExport function for compatibility with non-ESM modules
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
// webpack/runtime/define_property_getters
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
// webpack/runtime/has_own_property
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
// webpack/runtime/make_namespace_object
(()=>{
    // define __esModule on exports
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
/************************************************************************/ var __webpack_exports__ = {};
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// EXPORTS
__webpack_require__.d(__webpack_exports__, {
    default: ()=>/* binding */ ConfirmModal_rslib_entry_
});
const external_react_namespaceObject = require("react");
const react_namespaceObject = require("@chakra-ui/react");
const external_useConfirmModal_js_namespaceObject = require("./useConfirmModal.js");
var external_useConfirmModal_js_default = /*#__PURE__*/ __webpack_require__.n(external_useConfirmModal_js_namespaceObject);
const icons_namespaceObject = require("@chakra-ui/icons");
const utils_js_namespaceObject = require("../../utils/utils.js");
// default prop values
const DEFAULT_VALUES = {
    LABEL_CONFIRM: 'Confirm',
    LABEL_DELETE: 'Delete',
    LABEL_CLOSE: 'Close',
    LABEL_CANCEL: 'Cancel',
    VARIANT: 'default',
    NOOP: utils_js_namespaceObject.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, external_react_namespaceObject.useCallback)(async ()=>{
        await showModal().catch(()=>{
            onCancel();
        });
    }, []);
    const onCancel = (0, external_react_namespaceObject.useCallback)(()=>{
        onClose();
    }, [
        onClose
    ]);
    const onConfirmClick = (0, external_react_namespaceObject.useCallback)((modalConfirm)=>{
        if (autoCloseOnConfirm) modalConfirm();
        // call back to user
        onConfirm(modalConfirm);
    }, [
        onConfirm
    ]);
    const getHeadingElement = (0, external_react_namespaceObject.useCallback)(()=>{
        switch(variant){
            case 'success':
                return /*#__PURE__*/ React.createElement(react_namespaceObject.HStack, {
                    spacing: "0.625rem"
                }, /*#__PURE__*/ React.createElement(icons_namespaceObject.CheckCircleIcon, {
                    color: 'green.500'
                }), /*#__PURE__*/ React.createElement(react_namespaceObject.Heading, {
                    variant: "md"
                }, heading));
            case 'error':
                return /*#__PURE__*/ React.createElement(react_namespaceObject.HStack, {
                    spacing: "0.625rem"
                }, /*#__PURE__*/ React.createElement(icons_namespaceObject.WarningIcon, {
                    color: 'red.500'
                }), /*#__PURE__*/ React.createElement(react_namespaceObject.Heading, {
                    variant: "md"
                }, heading));
            default:
                return /*#__PURE__*/ React.createElement(react_namespaceObject.Heading, {
                    variant: "md"
                }, heading);
        }
    }, [
        variant,
        heading
    ]);
    const getPrimaryButtonLabel = (0, external_react_namespaceObject.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, external_react_namespaceObject.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, external_react_namespaceObject.useCallback)((confirm, cancel)=>{
        switch(variant){
            case 'default':
                return /*#__PURE__*/ React.createElement(react_namespaceObject.ButtonGroup, null, /*#__PURE__*/ React.createElement(react_namespaceObject.Button, {
                    variant: "ghost",
                    onClick: cancel
                }, getSecondaryButtonLabel()), /*#__PURE__*/ React.createElement(react_namespaceObject.Button, {
                    variant: "solid",
                    onClick: ()=>onConfirmClick(confirm)
                }, getPrimaryButtonLabel()));
            case 'delete':
                return /*#__PURE__*/ React.createElement(react_namespaceObject.ButtonGroup, null, /*#__PURE__*/ React.createElement(react_namespaceObject.Button, {
                    variant: "delete",
                    onClick: ()=>onConfirmClick(confirm)
                }, getPrimaryButtonLabel()), /*#__PURE__*/ React.createElement(react_namespaceObject.Button, {
                    variant: "ghost",
                    onClick: cancel
                }, getSecondaryButtonLabel()));
            case 'success':
            case 'error':
                return /*#__PURE__*/ React.createElement(react_namespaceObject.ButtonGroup, null, /*#__PURE__*/ React.createElement(react_namespaceObject.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, external_react_namespaceObject.useCallback)(()=>{
        if ('string' == typeof trigger) return /*#__PURE__*/ React.createElement(react_namespaceObject.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, external_react_namespaceObject.cloneElement)(trigger, {
            onClick: (event)=>{
                if (trigger.props.onClick) trigger.props.onClick(event);
                handleTriggerOnClick();
            }
        });
    }, [
        handleTriggerOnClick,
        trigger
    ]);
    const { renderModal, showModal } = external_useConfirmModal_js_default()({
        heading: getHeadingElement(),
        render: ({ confirm, cancel })=>/*#__PURE__*/ React.createElement(react_namespaceObject.Stack, {
                spacing: "4"
            }, /*#__PURE__*/ React.createElement(react_namespaceObject.Box, null, children), /*#__PURE__*/ React.createElement(react_namespaceObject.Box, {
                alignSelf: "end"
            }, getButtonElements(confirm, cancel)))
    });
    return /*#__PURE__*/ React.createElement(React.Fragment, null, getTriggerElement(), renderModal());
};
/* ESM default export */ const ConfirmModal_rslib_entry_ = ConfirmModal;
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
