"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timeline = void 0;
const date_fns_1 = require("date-fns");
const react_1 = __importStar(require("react"));
const consoleWarning_1 = require("../utils/helpers/consoleWarning");
const AxisLabels_1 = require("./AxisLabels");
const TimelineRow_1 = __importDefault(require("./TimelineRow"));
const TimelineKeyboardNavProvider_1 = require("./hooks/TimelineKeyboardNavProvider");
const useRowContext_1 = require("./hooks/useRowContext");
const useTimelineContext_1 = require("./hooks/useTimelineContext");
const useTimelineRows_1 = require("./hooks/useTimelineRows");
const period_1 = __importDefault(require("./period"));
const Pin_1 = __importDefault(require("./pin/Pin"));
const timeline_1 = require("./utils/timeline");
const zoom_1 = __importDefault(require("./zoom"));
/**
 * A component that displays a timeline of events. Meant for Internal systems.
 *
 * Component is made for desktop enviroments and will start having issues on smaller screens.
 *
 * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/timeline)
 * @see 🏷️ {@link TimelineProps}
 *
 * @example
 * ```jsx
 * <Timeline>
 *   <Timeline.Row>
 *    <Timeline.Period start={new Date("2020-01-01")} end={new Date("2020-01-31")}>
 *    <p>Period 1</p>
 *   </Timeline.Period>
 *  <Timeline.Row>
 * </Timeline>
 * ```
 */
exports.Timeline = (0, react_1.forwardRef)((_a, forwardedRef) => {
    var { children, startDate, endDate, direction = "left", axisLabelTemplates } = _a, rest = __rest(_a, ["children", "startDate", "endDate", "direction", "axisLabelTemplates"]);
    const childArray = (0, react_1.useMemo)(() => react_1.default.Children.toArray(Array.isArray(children) ? children : [children]), [children]);
    const rowChildren = (0, react_1.useMemo)(() => {
        return childArray.filter((c) => { var _a; return ((_a = c === null || c === void 0 ? void 0 : c.type) === null || _a === void 0 ? void 0 : _a.componentType) === "row"; });
    }, [childArray]);
    const pins = (0, react_1.useMemo)(() => childArray
        .filter((c) => { var _a; return ((_a = c === null || c === void 0 ? void 0 : c.type) === null || _a === void 0 ? void 0 : _a.componentType) === "pin"; })
        .map((x) => () => x), [childArray]);
    const zoomComponent = (0, react_1.useMemo)(() => childArray.find((c) => { var _a; return ((_a = c === null || c === void 0 ? void 0 : c.type) === null || _a === void 0 ? void 0 : _a.componentType) === "zoom"; }), [childArray]);
    const rowsRaw = (0, react_1.useMemo)(() => {
        return (0, timeline_1.parseRows)(rowChildren);
    }, [rowChildren]);
    const rows = (0, react_1.useMemo)(() => rowsRaw.map((r) => {
        if (r === null || r === void 0 ? void 0 : r.periods) {
            return r.periods;
        }
        return [];
    }), [rowsRaw]);
    const initialStartDate = (0, date_fns_1.startOfDay)((0, useTimelineRows_1.useEarliestDate)({ startDate, rows }));
    const [start, setStart] = (0, react_1.useState)(initialStartDate);
    const [endInclusive, setEndInclusive] = (0, react_1.useState)((0, date_fns_1.endOfDay)((0, useTimelineRows_1.useLatestDate)({ endDate, rows })));
    const initialEndDate = (0, date_fns_1.endOfDay)((0, useTimelineRows_1.useLatestDate)({ endDate, rows }));
    const processedRows = (0, useTimelineRows_1.useTimelineRows)(rowsRaw, startDate !== null && startDate !== void 0 ? startDate : start, endDate !== null && endDate !== void 0 ? endDate : endInclusive, direction);
    const handleZoomChange = (zoomStart) => {
        if (startDate || endDate) {
            (0, consoleWarning_1.consoleWarning)("<Timeline />: Zooming is not supported when `startDate` or `endDate` is set");
            return;
        }
        if (direction === "left") {
            if ((0, date_fns_1.isSameDay)(zoomStart, start)) {
                setStart(initialStartDate);
                return;
            }
            setStart(zoomStart);
        }
        else {
            if ((0, date_fns_1.isSameDay)(zoomStart, endInclusive)) {
                setEndInclusive(initialEndDate);
                return;
            }
            setEndInclusive(zoomStart);
        }
    };
    return (react_1.default.createElement(useTimelineContext_1.TimelineContext.Provider, { value: {
            startDate: startDate !== null && startDate !== void 0 ? startDate : start,
            endDate: endDate !== null && endDate !== void 0 ? endDate : endInclusive,
            direction,
            setStart: (d) => handleZoomChange(d),
            setEndInclusive: (d) => setEndInclusive(d),
        } },
        react_1.default.createElement(TimelineKeyboardNavProvider_1.TimelineKeyboardNavProvider, null,
            react_1.default.createElement("div", Object.assign({}, rest, { ref: forwardedRef }),
                react_1.default.createElement("div", { className: "aksel-timeline" },
                    react_1.default.createElement(AxisLabels_1.AxisLabels, { templates: axisLabelTemplates }),
                    pins.map((PinChild, i) => (react_1.default.createElement(PinChild, { key: `pin-${i}` }))),
                    processedRows.map((row, i) => {
                        return (react_1.default.createElement(useRowContext_1.RowContext.Provider, { key: `row-${row.id}`, value: {
                                periods: row.periods,
                                id: row.id,
                                index: i,
                            } },
                            react_1.default.createElement(TimelineRow_1.default, Object.assign({}, row === null || row === void 0 ? void 0 : row.restProps, { ref: row === null || row === void 0 ? void 0 : row.ref, label: row.label, icon: row.icon, headingTag: row.headingTag }))));
                    })),
                zoomComponent))));
});
exports.Timeline.Row = TimelineRow_1.default;
exports.Timeline.Period = period_1.default;
exports.Timeline.Pin = Pin_1.default;
exports.Timeline.Zoom = zoom_1.default;
exports.default = exports.Timeline;
//# sourceMappingURL=Timeline.js.map