"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.DialogPopup = exports.DialogFooter = exports.DialogBody = exports.DialogDescription = exports.DialogTitle = exports.DialogHeader = exports.DialogCloseTrigger = exports.DialogTrigger = exports.Dialog = void 0;
const react_1 = __importStar(require("react"));
const utils_external_1 = require("../../utils-external");
const hooks_1 = require("../../utils/hooks");
const DialogBody_1 = require("../body/DialogBody");
Object.defineProperty(exports, "DialogBody", { enumerable: true, get: function () { return DialogBody_1.DialogBody; } });
const DialogCloseTrigger_1 = require("../close-trigger/DialogCloseTrigger");
Object.defineProperty(exports, "DialogCloseTrigger", { enumerable: true, get: function () { return DialogCloseTrigger_1.DialogCloseTrigger; } });
const DialogDescription_1 = require("../description/DialogDescription");
Object.defineProperty(exports, "DialogDescription", { enumerable: true, get: function () { return DialogDescription_1.DialogDescription; } });
const DialogFooter_1 = require("../footer/DialogFooter");
Object.defineProperty(exports, "DialogFooter", { enumerable: true, get: function () { return DialogFooter_1.DialogFooter; } });
const DialogHeader_1 = require("../header/DialogHeader");
Object.defineProperty(exports, "DialogHeader", { enumerable: true, get: function () { return DialogHeader_1.DialogHeader; } });
const DialogPopup_1 = require("../popup/DialogPopup");
Object.defineProperty(exports, "DialogPopup", { enumerable: true, get: function () { return DialogPopup_1.DialogPopup; } });
const DialogTitle_1 = require("../title/DialogTitle");
Object.defineProperty(exports, "DialogTitle", { enumerable: true, get: function () { return DialogTitle_1.DialogTitle; } });
const DialogTrigger_1 = require("../trigger/DialogTrigger");
Object.defineProperty(exports, "DialogTrigger", { enumerable: true, get: function () { return DialogTrigger_1.DialogTrigger; } });
const DialogRoot_context_1 = require("./DialogRoot.context");
/**
 * Dialog component for displaying modal content on top of an application.
 * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/dialog)
 * @see 🏷️ {@link DialogProps}
 * @example
 * ```jsx
 *  <Dialog>
 *    <Dialog.Trigger>
 *      <Button>Open dialog</Button>
 *    </Dialog.Trigger>
 *    <Dialog.Popup>
 *      <Dialog.Header>
 *        <Dialog.Title>Dialog title</Dialog.Title>
 *        <Dialog.Description>Dialog description</Dialog.Description>
 *      </Dialog.Header>
 *      <Dialog.Body>
 *        Dialog body content
 *      </Dialog.Body>
 *      <Dialog.Footer>
 *        <Dialog.CloseTrigger>
 *          <Button>Close dialog</Button>
 *        </Dialog.CloseTrigger>
 *      </Dialog.Footer>
 *    </Dialog.Popup>
 *  </Dialog>
 * ```
 */
const Dialog = (props) => {
    var _a;
    const { children, defaultOpen = false, open: openParam, onOpenChange, onOpenChangeComplete, size = "medium", } = props;
    const [open, setOpenStateInternal] = (0, hooks_1.useControllableState)({
        defaultValue: defaultOpen,
        value: openParam,
    });
    const { mounted, setMounted, transitionStatus } = (0, hooks_1.useTransitionStatus)(open);
    const popupRef = (0, react_1.useRef)(null);
    const [triggerElement, setTriggerElement] = (0, react_1.useState)(null);
    const [popupElement, setPopupElement] = (0, react_1.useState)(null);
    const defaultId = (0, utils_external_1.useId)();
    const [titleId, setTitleId] = (0, react_1.useState)();
    const [ownNestedOpenDialogs, setOwnNestedOpenDialogs] = (0, react_1.useState)(0);
    const nestedDialogOpened = (0, react_1.useCallback)((nestedCount) => {
        setOwnNestedOpenDialogs(nestedCount + 1);
    }, []);
    const nestedDialogClosed = (0, react_1.useCallback)(() => {
        setOwnNestedOpenDialogs(0);
    }, []);
    const parentContext = (0, DialogRoot_context_1.useDialogContext)(false);
    /**
     * Notify parent dialog about nested dialogs opening/closing.
     * This allows us to better hide/obscure parent dialogs when nested dialogs are opened.
     *
     * This pattern is not good for deep nesting since the context updates will cause cascading renders
     * but should work fine for 1-2 levels of nesting which is the most common use case here.
     */
    (0, react_1.useEffect)(() => {
        if (open && parentContext) {
            parentContext.nestedDialogOpened(ownNestedOpenDialogs);
            return () => parentContext.nestedDialogClosed();
        }
    }, [open, parentContext, ownNestedOpenDialogs]);
    /**
     * Passing the original event to onOpenChange to allow preventing the state change
     */
    const setOpen = (0, hooks_1.useEventCallback)((nextOpen, originalEvent) => {
        onOpenChange === null || onOpenChange === void 0 ? void 0 : onOpenChange(nextOpen, originalEvent);
        if (originalEvent === null || originalEvent === void 0 ? void 0 : originalEvent.defaultPrevented) {
            return;
        }
        setOpenStateInternal(nextOpen);
    });
    return (react_1.default.createElement(DialogRoot_context_1.DialogContextProvider, { open: open, setOpen: setOpen, mounted: mounted, transitionStatus: transitionStatus, popupRef: popupRef, setPopupElement: setPopupElement, popupElement: popupElement, popupId: (_a = popupElement === null || popupElement === void 0 ? void 0 : popupElement.id) !== null && _a !== void 0 ? _a : defaultId, setTriggerElement: setTriggerElement, triggerElement: triggerElement, nested: !!parentContext, nestedDialogOpened: nestedDialogOpened, nestedDialogClosed: nestedDialogClosed, nestedOpenDialogCount: ownNestedOpenDialogs, size: size, titleId: titleId, setTitleId: setTitleId, onOpenChangeComplete: onOpenChangeComplete, setMounted: setMounted }, children));
};
exports.Dialog = Dialog;
exports.Dialog.Trigger = DialogTrigger_1.DialogTrigger;
exports.Dialog.CloseTrigger = DialogCloseTrigger_1.DialogCloseTrigger;
exports.Dialog.Header = DialogHeader_1.DialogHeader;
exports.Dialog.Title = DialogTitle_1.DialogTitle;
exports.Dialog.Description = DialogDescription_1.DialogDescription;
exports.Dialog.Body = DialogBody_1.DialogBody;
exports.Dialog.Footer = DialogFooter_1.DialogFooter;
exports.Dialog.Popup = DialogPopup_1.DialogPopup;
exports.default = exports.Dialog;
//# sourceMappingURL=DialogRoot.js.map