"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const exportTabs_1 = require("./exportTabs");
const tab = (uuid, order, hidden) => ({
    uuid,
    name: `Tab ${uuid}`,
    order,
    hidden,
});
describe('resolveExportTabs', () => {
    const tabs = [tab('c', 2), tab('a', 0), tab('b', 1, true)];
    it('returns all non-hidden tabs in order when selectedTabs is null', () => {
        expect((0, exportTabs_1.resolveExportTabs)(tabs, null).map((t) => t.uuid)).toEqual([
            'a',
            'c',
        ]);
    });
    it('returns explicitly selected tabs in dashboard order, including hidden', () => {
        expect((0, exportTabs_1.resolveExportTabs)(tabs, ['c', 'b']).map((t) => t.uuid)).toEqual([
            'b',
            'c',
        ]);
    });
    it('ignores selected uuids that no longer exist', () => {
        expect((0, exportTabs_1.resolveExportTabs)(tabs, ['a', 'deleted']).map((t) => t.uuid)).toEqual(['a']);
    });
    it('returns empty array for untabbed dashboards', () => {
        expect((0, exportTabs_1.resolveExportTabs)([], null)).toEqual([]);
    });
    it('does not mutate the input array', () => {
        const input = [tab('c', 2), tab('a', 0)];
        const snapshot = [...input];
        (0, exportTabs_1.resolveExportTabs)(input, null);
        expect(input).toEqual(snapshot);
    });
});
describe('sortTilesByDashboardOrder', () => {
    const tile = (id, tabUuid, y, x) => ({ id, tabUuid, y, x });
    it('sorts by tab order first, then y, then x', () => {
        const tabs = [tab('second', 1), tab('first', 0)];
        const tiles = [
            tile('s1', 'second', 0, 0),
            tile('f3', 'first', 1, 0),
            tile('f2', 'first', 0, 5),
            tile('f1', 'first', 0, 2),
        ];
        expect((0, exportTabs_1.sortTilesByDashboardOrder)(tiles, tabs).map((t) => t.id)).toEqual(['f1', 'f2', 'f3', 's1']);
    });
    it('interleaves orphan tiles with the first tab by position', () => {
        const tabs = [tab('a', 0), tab('b', 1)];
        const tiles = [
            tile('b1', 'b', 0, 0),
            tile('orphan', null, 0, 0),
            tile('a1', 'a', 1, 0),
        ];
        expect((0, exportTabs_1.sortTilesByDashboardOrder)(tiles, tabs).map((t) => t.id)).toEqual(['orphan', 'a1', 'b1']);
    });
    it('sorts untabbed dashboards by y then x', () => {
        const tiles = [
            tile('c', null, 1, 0),
            tile('a', null, 0, 0),
            tile('b', null, 0, 3),
        ];
        expect((0, exportTabs_1.sortTilesByDashboardOrder)(tiles, []).map((t) => t.id)).toEqual([
            'a',
            'b',
            'c',
        ]);
    });
    it('does not mutate the input array', () => {
        const tiles = [tile('b', null, 1, 0), tile('a', null, 0, 0)];
        const snapshot = [...tiles];
        (0, exportTabs_1.sortTilesByDashboardOrder)(tiles, []);
        expect(tiles).toEqual(snapshot);
    });
});
describe('getPagedExportOrphanHomeTabUuid', () => {
    it('returns the first resolved tab uuid', () => {
        expect((0, exportTabs_1.getPagedExportOrphanHomeTabUuid)(['a', 'c'])).toBe('a');
    });
    it('returns null when nothing is resolved', () => {
        expect((0, exportTabs_1.getPagedExportOrphanHomeTabUuid)([])).toBeNull();
    });
});
describe('isTileInPagedExport', () => {
    it('renders a tabbed tile only when its tab is resolved', () => {
        expect((0, exportTabs_1.isTileInPagedExport)({ tabUuid: 'a' }, ['a', 'c'])).toBe(true);
        expect((0, exportTabs_1.isTileInPagedExport)({ tabUuid: 'b' }, ['a', 'c'])).toBe(false);
    });
    it('renders orphan tiles when there is an orphan-home tab', () => {
        expect((0, exportTabs_1.isTileInPagedExport)({ tabUuid: null }, ['a', 'c'])).toBe(true);
        expect((0, exportTabs_1.isTileInPagedExport)({ tabUuid: undefined }, ['a'])).toBe(true);
    });
    it('drops orphan tiles when no tab is resolved', () => {
        expect((0, exportTabs_1.isTileInPagedExport)({ tabUuid: null }, [])).toBe(false);
    });
});
// Fix 3: orphan-home must be the first RENDER-ELIGIBLE tab, not the literal
// first dashboard tab, so orphans survive a hidden/unselected first tab.
describe('paged-export orphan-home resolution (resolveExportTabs + helpers)', () => {
    const resolved = (tabs, selection) => (0, exportTabs_1.resolveExportTabs)(tabs, selection).map((t) => t.uuid);
    it('null selection with a hidden first tab homes orphans on the first visible tab', () => {
        const tabs = [tab('a', 0, true), tab('b', 1), tab('c', 2)];
        const uuids = resolved(tabs, null);
        expect(uuids).toEqual(['b', 'c']);
        expect((0, exportTabs_1.getPagedExportOrphanHomeTabUuid)(uuids)).toBe('b');
        expect((0, exportTabs_1.isTileInPagedExport)({ tabUuid: null }, uuids)).toBe(true);
    });
    it('explicit subset excluding tab 1 homes orphans on the first selected tab', () => {
        const tabs = [tab('a', 0), tab('b', 1), tab('c', 2)];
        const uuids = resolved(tabs, ['b', 'c']);
        expect(uuids).toEqual(['b', 'c']);
        expect((0, exportTabs_1.getPagedExportOrphanHomeTabUuid)(uuids)).toBe('b');
        expect((0, exportTabs_1.isTileInPagedExport)({ tabUuid: null }, uuids)).toBe(true);
    });
    it('orphan-only dashboard (no tabs) has no orphan-home and excludes orphans from the paged set', () => {
        const uuids = resolved([], null);
        expect(uuids).toEqual([]);
        expect((0, exportTabs_1.getPagedExportOrphanHomeTabUuid)(uuids)).toBeNull();
        expect((0, exportTabs_1.isTileInPagedExport)({ tabUuid: null }, uuids)).toBe(false);
    });
});
//# sourceMappingURL=exportTabs.test.js.map