import React__default, { forwardRef, useReducer, useImperativeHandle, useEffect } from 'react';
import { useOnKeyDown } from '@jobber/hooks';
import { Modal } from './Modal/index.mjs';
import { C as Content } from './Content-es.js';
import { M as Markdown } from './Markdown-es.js';

function confirmationModalReducer(state, action) {
    switch (action.type) {
        case "display":
            return Object.assign(Object.assign({}, state), { title: action.title, message: action.message, open: true, confirmLabel: action.confirmLabel, cancelLabel: action.cancelLabel || "Cancel", onConfirm: action.onConfirm, onCancel: action.onCancel });
        case "confirm":
            return Object.assign(Object.assign({}, state), { open: false });
        case "cancel":
            return Object.assign(Object.assign({}, state), { open: false });
        case "reset":
            return Object.assign(Object.assign({}, state), action.state);
        default:
            throw new Error();
    }
}
const ConfirmationModal = forwardRef(function ConfirmationModalInternal({ title, message, open = false, confirmLabel = "Confirm", cancelLabel = "Cancel", onConfirm, onCancel, onRequestClose, variation = "work", size, children, }, ref) {
    const [state, dispatch] = useReducer(confirmationModalReducer, {
        title,
        message,
        open: false,
        confirmLabel,
        cancelLabel,
        onConfirm,
        onCancel,
    });
    // Expose a `show` method through this component's ref.
    useImperativeHandle(ref, () => ({
        show: ({ title: newTitle, message: newMessage, confirmLabel: newConfirmLabel, cancelLabel: newCancelLabel, onConfirm: newOnConfirm, onCancel: newOnCancel, }) => {
            dispatch({
                type: "display",
                title: newTitle,
                message: newMessage,
                confirmLabel: newConfirmLabel,
                cancelLabel: newCancelLabel,
                onConfirm: newOnConfirm,
                onCancel: newOnCancel,
            });
        },
    }));
    useEffect(() => {
        dispatch({
            type: "reset",
            state: Object.assign(Object.assign({}, state), { title, message: message, confirmLabel,
                cancelLabel,
                onConfirm,
                onCancel }),
        });
    }, [title, message, confirmLabel, cancelLabel, onConfirm, onCancel]);
    useOnKeyDown(handleKeyboardShortcut, ["Escape", "Enter"]);
    return (React__default.createElement(Modal, { title: state.title, open: open || state.open, size: size, dismissible: false, primaryAction: {
            label: state.confirmLabel,
            onClick: handleAction("confirm"),
            variation: variation === "destructive" ? "destructive" : "work",
        }, secondaryAction: {
            label: state.cancelLabel,
            onClick: handleAction("cancel"),
        } }, state.message ? (React__default.createElement(Content, null,
        React__default.createElement(Markdown, { content: state.message }))) : (children)));
    function handleAction(type) {
        return () => {
            var _a, _b;
            if (type === "confirm") {
                (_a = state.onConfirm) === null || _a === void 0 ? void 0 : _a.call(state);
            }
            else if (type === "cancel") {
                (_b = state.onCancel) === null || _b === void 0 ? void 0 : _b.call(state);
            }
            dispatch({ type });
            onRequestClose && onRequestClose();
        };
    }
    function handleKeyboardShortcut(event) {
        const { metaKey, ctrlKey, key, target } = event;
        if (!open)
            return;
        const shouldTriggerShortcut = target instanceof HTMLButtonElement ? metaKey || ctrlKey : true;
        if (!shouldTriggerShortcut)
            return;
        event.preventDefault();
        event.stopPropagation();
        switch (key) {
            case "Enter": {
                handleAction("confirm")();
                break;
            }
            case "Escape": {
                handleAction("cancel")();
                break;
            }
        }
    }
});

export { ConfirmationModal as C };
