"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.AxisLabels = exports.yearLabels = exports.monthLabels = exports.dayLabels = void 0;
const date_fns_1 = require("date-fns");
const react_1 = __importStar(require("react"));
const Detail_1 = require("../typography/Detail");
const i18n_hooks_1 = require("../utils/i18n/i18n.hooks");
const useTimelineContext_1 = require("./hooks/useTimelineContext");
const utils_1 = require("./utils");
const calc_1 = require("./utils/calc");
const dayLabels = (start, end, totalDays, direction, template, locale) => {
    const increment = Math.ceil(totalDays / 10);
    const lastDay = (0, date_fns_1.startOfDay)(end);
    const count = Math.ceil(totalDays / increment);
    return Array.from({ length: count }, (_, i) => {
        const day = (0, date_fns_1.subDays)(lastDay, i * increment);
        const { horizontalPosition, width } = (0, calc_1.horizontalPositionAndWidth)(day, (0, date_fns_1.addDays)(day, 1), start, end);
        return {
            direction,
            horizontalPosition,
            label: (0, date_fns_1.format)(day, template, { locale }),
            date: day,
            width,
        };
    });
};
exports.dayLabels = dayLabels;
const monthLabels = (start, end, direction, template, locale) => {
    const startMonth = (0, date_fns_1.startOfMonth)(start);
    const endMonth = (0, date_fns_1.endOfMonth)(end);
    const numberOfMonths = (0, date_fns_1.differenceInMonths)(endMonth, startMonth) + 1;
    return Array.from({ length: numberOfMonths }, (_, i) => {
        const month = (0, date_fns_1.addMonths)(startMonth, i);
        const { horizontalPosition, width } = (0, calc_1.horizontalPositionAndWidth)(month, (0, date_fns_1.addMonths)(month, 1), start, end);
        return {
            direction,
            horizontalPosition,
            label: (0, date_fns_1.format)(month, template, { locale }),
            date: month,
            width,
        };
    });
};
exports.monthLabels = monthLabels;
const yearLabels = (start, end, direction, template, locale) => {
    const firstYear = (0, date_fns_1.startOfYear)(start);
    const lastYear = (0, date_fns_1.endOfYear)(end);
    const yearCount = (0, date_fns_1.differenceInYears)(lastYear, start) + 1;
    return Array.from({ length: yearCount }, (_, i) => {
        const year = (0, date_fns_1.addYears)(firstYear, i);
        const { horizontalPosition, width } = (0, calc_1.horizontalPositionAndWidth)(year, (0, date_fns_1.addYears)(year, 1), start, end);
        return {
            direction,
            horizontalPosition,
            label: (0, date_fns_1.format)(year, template, { locale }),
            date: year,
            width,
        };
    });
};
exports.yearLabels = yearLabels;
const getLabels = (start, end, direction, locale, translate) => {
    const totalDays = (0, date_fns_1.differenceInDays)(end, start);
    /* Zero or negative total days can break further calculations */
    if (totalDays <= 0) {
        return [];
    }
    if (totalDays < 40) {
        const dayTemplate = translate("dayFormat");
        return (0, exports.dayLabels)(start, end, totalDays, direction, dayTemplate, locale);
    }
    if (totalDays < 370) {
        const monthTemplate = translate("monthFormat");
        return (0, exports.monthLabels)(start, end, direction, monthTemplate, locale);
    }
    const yearTemplate = translate("yearFormat");
    return (0, exports.yearLabels)(start, end, direction, yearTemplate, locale);
};
const AxisLabels = ({ templates, }) => {
    const { endDate, startDate, direction } = (0, useTimelineContext_1.useTimelineContext)();
    const translate = (0, i18n_hooks_1.useI18n)("Timeline", {
        dayFormat: templates === null || templates === void 0 ? void 0 : templates.day,
        monthFormat: templates === null || templates === void 0 ? void 0 : templates.month,
        yearFormat: templates === null || templates === void 0 ? void 0 : templates.year,
    });
    const locale = (0, i18n_hooks_1.useDateLocale)();
    const labels = (0, react_1.useMemo)(() => getLabels(startDate, endDate, direction, locale, translate)
        .filter(utils_1.isVisible)
        .sort((a, b) => a.horizontalPosition - b.horizontalPosition), [startDate, endDate, direction, locale, translate]);
    const getLabelWidth = (0, react_1.useCallback)((index) => {
        const next = labels[index + 1];
        const bound = next ? next.horizontalPosition : 100;
        return `${(bound - labels[index].horizontalPosition).toFixed(3)}%`;
    }, [labels]);
    const justifyContent = direction === "left" ? "flex-start" : "flex-end";
    return (react_1.default.createElement("div", { className: "aksel-timeline__axislabels", "aria-hidden": "true" }, labels.map((etikett, index) => (react_1.default.createElement(Detail_1.Detail, { className: "aksel-timeline__axislabels-label", as: "div", key: etikett.label, style: {
            justifyContent,
            [direction]: `${etikett.horizontalPosition.toFixed(3)}%`,
            width: getLabelWidth(index),
        } }, etikett.label)))));
};
exports.AxisLabels = AxisLabels;
//# sourceMappingURL=AxisLabels.js.map