"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;
    };
})();
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PinInternal = void 0;
const react_1 = require("@floating-ui/react");
const date_fns_1 = require("date-fns");
const react_2 = __importStar(require("react"));
const hooks_1 = require("../../utils/hooks");
const i18n_hooks_1 = require("../../utils/i18n/i18n.hooks");
const TimelineKeyboardNavProvider_1 = require("../hooks/TimelineKeyboardNavProvider");
const useTimelineContext_1 = require("../hooks/useTimelineContext");
const calc_1 = require("../utils/calc");
exports.PinInternal = (0, react_2.forwardRef)((_a, ref) => {
    var { date, children } = _a, rest = __rest(_a, ["date", "children"]);
    const { startDate, endDate, direction } = (0, useTimelineContext_1.useTimelineContext)();
    const { handlePinKeyDown } = (0, TimelineKeyboardNavProvider_1.useTimelineKeyboardContext)();
    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: "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, ref);
    const label = translate("Pin.pin", {
        date: (0, date_fns_1.format)(date, translate("dateFormat")),
    });
    const pinPositionStyle = (0, react_2.useMemo)(() => {
        const pinPosition = (0, calc_1.position)(date, startDate, endDate);
        return Number.isFinite(pinPosition) ? `${pinPosition.toFixed(3)}%` : "0%";
    }, [date, endDate, startDate]);
    return (react_2.default.createElement(react_2.default.Fragment, null,
        react_2.default.createElement("div", { className: "aksel-timeline__pin-wrapper", style: { [direction]: pinPositionStyle } },
            react_2.default.createElement("button", Object.assign({ "data-timeline-pin": true }, rest, { ref: mergedRef, className: "aksel-timeline__pin-button", "aria-label": label, type: "button", "aria-expanded": children ? open : undefined }, getReferenceProps({
                onKeyDown: (e) => {
                    var _a;
                    (_a = rest === null || rest === void 0 ? void 0 : rest.onKeyDown) === null || _a === void 0 ? void 0 : _a.call(rest, e);
                    handlePinKeyDown(e);
                },
            })))),
        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 = exports.PinInternal;
//# sourceMappingURL=PinInternal.js.map