"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 */ ConfirmItemDeleteModal_rslib_entry_
});
const external_react_namespaceObject = require("react");
const external_useConfirmModal_js_namespaceObject = require("./useConfirmModal.js");
var external_useConfirmModal_js_default = /*#__PURE__*/ __webpack_require__.n(external_useConfirmModal_js_namespaceObject);
const Button_js_namespaceObject = require("../Button/Button.js");
var Button_js_default = /*#__PURE__*/ __webpack_require__.n(Button_js_namespaceObject);
const react_namespaceObject = require("@chakra-ui/react");
const FormField_js_namespaceObject = require("../FormField/FormField.js");
var FormField_js_default = /*#__PURE__*/ __webpack_require__.n(FormField_js_namespaceObject);
const utils_js_namespaceObject = require("../../utils/utils.js");
// default prop values
const DEFAULT_VALUES = {
    LABEL_DELETE: 'Delete',
    LABEL_CANCEL: 'Cancel',
    INPUT_MATCH_TEXT: 'delete',
    TEXT_DELETE_INSTRUCTIONS: 'This action will cause data loss. To confirm, type "%input_match_text%" into the confirmation box below.',
    NOOP: utils_js_namespaceObject.noop
};
const ConfirmItemDeleteModal = (props)=>{
    const { trigger, itemType, primaryActionLabel, secondaryActionLabel, inputMatchText = DEFAULT_VALUES.INPUT_MATCH_TEXT, onConfirm, onClose = DEFAULT_VALUES.NOOP, heading = `${DEFAULT_VALUES.LABEL_DELETE} ${itemType}`, children = /*#__PURE__*/ React.createElement(react_namespaceObject.Text, null, DEFAULT_VALUES.TEXT_DELETE_INSTRUCTIONS.replace(/%input_match_text%/, inputMatchText)), requireValidation = true } = props;
    const [valid, setValid] = (0, external_react_namespaceObject.useState)('');
    const [isDeleting, setIsDeleting] = (0, external_react_namespaceObject.useState)(false);
    const [deleteErrorMessage, setDeleteErrorMessage] = (0, external_react_namespaceObject.useState)(null);
    // callback to displaying any errors that might occur during a delete operation
    const onError = (0, external_react_namespaceObject.useCallback)((msg)=>{
        setIsDeleting(false);
        setDeleteErrorMessage(msg);
    }, []);
    const resetState = (0, external_react_namespaceObject.useCallback)(()=>{
        setValid('');
        setIsDeleting(false);
        setDeleteErrorMessage(null);
    }, []);
    const onCancel = (0, external_react_namespaceObject.useCallback)(()=>{
        onClose();
    }, [
        onClose
    ]);
    const handleOnClick = (0, external_react_namespaceObject.useCallback)(async ()=>{
        // clear any previous state before showing modal
        resetState();
        await showModal().catch(()=>onCancel());
    }, []);
    // allows for both custom components or simple text labels;
    // 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(Button_js_default(), {
            variant: "delete",
            "data-testid": "trigger-delete-button",
            onClick: handleOnClick
        }, 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)=>{
                trigger.props.onClick?.(event);
                handleOnClick();
            }
        });
    }, [
        handleOnClick,
        trigger
    ]);
    const getPrimaryButtonLabel = (0, external_react_namespaceObject.useCallback)(()=>primaryActionLabel || DEFAULT_VALUES.LABEL_DELETE, [
        primaryActionLabel
    ]);
    const getSecondaryButtonLabel = (0, external_react_namespaceObject.useCallback)(()=>secondaryActionLabel || DEFAULT_VALUES.LABEL_CANCEL, [
        secondaryActionLabel
    ]);
    const inputTextDoesNotMatch = (0, external_react_namespaceObject.useCallback)((text)=>text.toLowerCase() !== inputMatchText.toLowerCase(), []);
    const { showModal, renderModal } = external_useConfirmModal_js_default()({
        heading,
        render: ({ confirm, cancel })=>/*#__PURE__*/ React.createElement(react_namespaceObject.Stack, {
                spacing: "4"
            }, children, /*#__PURE__*/ React.createElement(FormField_js_default(), {
                label: "" // Note: doesn't currently support react element
                ,
                isInvalid: '' !== valid && inputTextDoesNotMatch(valid),
                errorText: `Text must match "${inputMatchText}"`
            }, /*#__PURE__*/ React.createElement(react_namespaceObject.Input, {
                type: "text",
                name: "name",
                "data-testid": "txt-confirmation-delete",
                placeholder: `Type "${inputMatchText}" to confirm`,
                onChange: (v)=>setValid(v.target.value)
            })), deleteErrorMessage && /*#__PURE__*/ React.createElement(react_namespaceObject.Text, {
                color: 'error.500'
            }, deleteErrorMessage), /*#__PURE__*/ React.createElement(react_namespaceObject.Box, {
                alignSelf: "end"
            }, /*#__PURE__*/ React.createElement(react_namespaceObject.ButtonGroup, null, /*#__PURE__*/ React.createElement(Button_js_default(), {
                variant: "delete",
                "data-testid": "test-delete-item",
                id: "delete-modal-btn",
                onClick: ()=>{
                    setIsDeleting(true);
                    setDeleteErrorMessage(null);
                    onConfirm(confirm, onError);
                },
                isLoading: isDeleting,
                isDisabled: props.isDisabled || '' === valid || requireValidation && inputTextDoesNotMatch(valid)
            }, getPrimaryButtonLabel()), /*#__PURE__*/ React.createElement(Button_js_default(), {
                variant: "ghost",
                "data-testid": "test-cancel-item",
                onClick: cancel
            }, getSecondaryButtonLabel()))))
    });
    return /*#__PURE__*/ React.createElement(React.Fragment, null, getTriggerElement(), renderModal());
};
/* ESM default export */ const ConfirmItemDeleteModal_rslib_entry_ = ConfirmItemDeleteModal;
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
});
