"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 });
const react_1 = require("@floating-ui/react");
const react_2 = __importStar(require("react"));
const helpers_1 = require("../../utils/helpers");
const hooks_1 = require("../../utils/hooks");
const i18n_hooks_1 = require("../../utils/i18n/i18n.hooks");
const period_1 = require("../utils/period");
const ClickablePeriod = react_2.default.memo(({ onSelectPeriod, start, end, status, cropped, direction, left, width, icon, children, isActive, statusLabel, restProps, periodRef, }) => {
    var _a, _b;
    const [open, setOpen] = (0, react_2.useState)(false);
    const translate = (0, i18n_hooks_1.useI18n)("Timeline");
    const { context, placement, refs, floatingStyles } = (0, react_1.useFloating)({
        placement: (_a = restProps === null || restProps === void 0 ? void 0 : restProps.placement) !== null && _a !== void 0 ? _a : "top",
        open,
        onOpenChange: (_open) => setOpen(_open),
        whileElementsMounted: react_1.autoUpdate,
        middleware: [
            (0, react_1.offset)(8),
            (0, react_1.shift)(),
            (0, react_1.flip)({ padding: 5, fallbackPlacements: ["bottom", "top"] }),
        ],
    });
    const hover = (0, react_1.useHover)(context, {
        handleClose: (0, react_1.safePolygon)(),
        restMs: 25,
        delay: { open: 1000 },
        move: false,
    });
    const focus = (0, react_1.useFocus)(context);
    const dismiss = (0, react_1.useDismiss)(context);
    const click = (0, react_1.useClick)(context);
    const { getFloatingProps, getReferenceProps } = (0, react_1.useInteractions)([
        click,
        hover,
        focus,
        dismiss,
    ]);
    const mergedRef = (0, hooks_1.useMergeRefs)(refs.setReference, periodRef);
    const label = (0, period_1.ariaLabel)(start, end, status, statusLabel, translate);
    if (width === 0 || !Number.isFinite(width)) {
        return null;
    }
    if (!Number.isFinite(left)) {
        return null;
    }
    return (react_2.default.createElement(react_2.default.Fragment, null,
        react_2.default.createElement("button", Object.assign({ "data-timeline-period": true }, restProps, { "data-color": (_b = restProps === null || restProps === void 0 ? void 0 : restProps["data-color"]) !== null && _b !== void 0 ? _b : status, type: "button", ref: mergedRef, "aria-label": label, className: (0, helpers_1.cl)("aksel-timeline__period--clickable", (0, period_1.getConditionalClasses)(cropped, direction, status), restProps === null || restProps === void 0 ? void 0 : restProps.className, {
                "aksel-timeline__period--selected": isActive,
            }), "aria-expanded": children ? open : undefined, "aria-current": isActive || undefined }, getReferenceProps({
            onKeyDown: (e) => {
                var _a;
                (_a = restProps === null || restProps === void 0 ? void 0 : restProps.onKeyDown) === null || _a === void 0 ? void 0 : _a.call(restProps, e);
                if (e.key === " ") {
                    onSelectPeriod === null || onSelectPeriod === void 0 ? void 0 : onSelectPeriod(e);
                }
            },
            style: {
                width: `${width.toFixed(3)}%`,
                [direction]: `${left.toFixed(3)}%`,
            },
            onClick: (e) => {
                var _a;
                (_a = restProps === null || restProps === void 0 ? void 0 : restProps.onClick) === null || _a === void 0 ? void 0 : _a.call(restProps, e);
                if (e.detail === 0) {
                    return;
                }
                onSelectPeriod === null || onSelectPeriod === void 0 ? void 0 : onSelectPeriod(e);
            },
        })),
            react_2.default.createElement("span", { className: "aksel-timeline__period--inner" }, icon)),
        children && open && (react_2.default.createElement(react_1.FloatingFocusManager, { context: context, modal: false, initialFocus: -1, returnFocus: true },
            react_2.default.createElement("div", Object.assign({ className: "aksel-timeline__popover", "data-timeline-popover": true, "data-placement": placement, ref: refs.setFloating, role: "dialog", "aria-label": label }, getFloatingProps(), { style: floatingStyles }), children)))));
});
exports.default = ClickablePeriod;
//# sourceMappingURL=ClickablePeriod.js.map