"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.DateDialog = void 0;
const react_1 = __importStar(require("react"));
const button_1 = require("../button");
const DialogRoot_context_1 = require("../dialog/root/DialogRoot.context");
const modal_1 = require("../modal");
const Modal_context_1 = require("../modal/Modal.context");
const popover_1 = require("../popover");
const helpers_1 = require("../utils/helpers");
const focus_1 = require("../utils/helpers/focus");
const hooks_1 = require("../utils/hooks");
const i18n_hooks_1 = require("../utils/i18n/i18n.hooks");
const Date_locale_1 = require("./Date.locale");
const variantToLabel = {
    single: "chooseDate",
    multiple: "chooseDates",
    range: "chooseDateRange",
    month: "chooseMonth",
};
const DateDialog = ({ open, children, onClose, anchor, locale, translate, variant, popoverProps, popupLabelId, }) => {
    const translateGlobal = (0, i18n_hooks_1.useI18n)("global", (0, Date_locale_1.getGlobalTranslations)(locale));
    const modalRef = (0, react_1.useRef)(null);
    const isInModal = (0, Modal_context_1.useModalContext)(false) !== undefined;
    const isInDialog = (0, DialogRoot_context_1.useDialogContext)(false) !== undefined;
    const hideModal = (0, hooks_1.useMedia)("screen and (min-width: 768px)", true) &&
        !isInModal &&
        !isInDialog;
    if (!open) {
        return null;
    }
    if (hideModal) {
        return (react_1.default.createElement(popover_1.Popover, Object.assign({ anchorEl: anchor, open: open, onClose: onClose, placement: "bottom-start", className: (0, helpers_1.cl)("aksel-date__popover", {
                "aksel-date": variant === "month",
            }) }, popoverProps, { ref: focusPopoverOnOpen, tabIndex: -1, role: "dialog", "aria-labelledby": popupLabelId }), children));
    }
    return (react_1.default.createElement(modal_1.Modal, { ref: modalRef, open: open, onClose: (event) => {
            event.stopPropagation();
            onClose();
        }, "aria-label": translate(variantToLabel[variant]), className: (0, helpers_1.cl)("aksel-date__modal", {
            "aksel-date__nested-modal": isInModal,
            "aksel-date": variant === "month",
        }), closeOnBackdropClick: true, placement: "top" },
        react_1.default.createElement("div", { className: "aksel-date__modal-body" },
            children,
            react_1.default.createElement(button_1.Button, { variant: "tertiary", onClick: () => { var _a; return (_a = modalRef === null || modalRef === void 0 ? void 0 : modalRef.current) === null || _a === void 0 ? void 0 : _a.close(); }, size: "small", type: "button" }, translateGlobal("close")))));
};
exports.DateDialog = DateDialog;
function focusPopoverOnOpen(element) {
    (0, focus_1.focusElement)(element, { preventScroll: true, sync: false });
}
//# sourceMappingURL=Date.Dialog.js.map