import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__useConfirmModal_mjs__ from "./useConfirmModal.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Button_Button_mjs__ from "../Button/Button.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__chakra_ui_react__ from "@chakra-ui/react";
import * as __WEBPACK_EXTERNAL_MODULE__FormField_FormField_mjs__ from "../FormField/FormField.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_utils_mjs__ from "../../utils/utils.mjs";
// 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: __WEBPACK_EXTERNAL_MODULE__utils_utils_mjs__.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(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Text, null, DEFAULT_VALUES.TEXT_DELETE_INSTRUCTIONS.replace(/%input_match_text%/, inputMatchText)), requireValidation = true } = props;
    const [valid, setValid] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)('');
    const [isDeleting, setIsDeleting] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const [deleteErrorMessage, setDeleteErrorMessage] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(null);
    // callback to displaying any errors that might occur during a delete operation
    const onError = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((msg)=>{
        setIsDeleting(false);
        setDeleteErrorMessage(msg);
    }, []);
    const resetState = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        setValid('');
        setIsDeleting(false);
        setDeleteErrorMessage(null);
    }, []);
    const onCancel = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        onClose();
    }, [
        onClose
    ]);
    const handleOnClick = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        if ('string' == typeof trigger) return /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__Button_Button_mjs__["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, __WEBPACK_EXTERNAL_MODULE_react__.cloneElement)(trigger, {
            onClick: (event)=>{
                trigger.props.onClick?.(event);
                handleOnClick();
            }
        });
    }, [
        handleOnClick,
        trigger
    ]);
    const getPrimaryButtonLabel = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>primaryActionLabel || DEFAULT_VALUES.LABEL_DELETE, [
        primaryActionLabel
    ]);
    const getSecondaryButtonLabel = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>secondaryActionLabel || DEFAULT_VALUES.LABEL_CANCEL, [
        secondaryActionLabel
    ]);
    const inputTextDoesNotMatch = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((text)=>text.toLowerCase() !== inputMatchText.toLowerCase(), []);
    const { showModal, renderModal } = (0, __WEBPACK_EXTERNAL_MODULE__useConfirmModal_mjs__["default"])({
        heading,
        render: ({ confirm, cancel })=>/*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Stack, {
                spacing: "4"
            }, children, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__FormField_FormField_mjs__["default"], {
                label: "" // Note: doesn't currently support react element
                ,
                isInvalid: '' !== valid && inputTextDoesNotMatch(valid),
                errorText: `Text must match "${inputMatchText}"`
            }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.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(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Text, {
                color: 'error.500'
            }, deleteErrorMessage), /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Box, {
                alignSelf: "end"
            }, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.ButtonGroup, null, /*#__PURE__*/ React.createElement(__WEBPACK_EXTERNAL_MODULE__Button_Button_mjs__["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(__WEBPACK_EXTERNAL_MODULE__Button_Button_mjs__["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;
export { ConfirmItemDeleteModal_rslib_entry_ as default };
