/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Base
 */
import { UiError } from "@itwin/appui-abstract";
import { produce } from "immer";
import { isHorizontalPanelSide } from "../../widget-panels/Panel.js";
import { category } from "./NineZoneStateHelpers.js";
import { addWidgetState } from "./WidgetStateHelpers.js";
function createPanelState(side) {
    return {
        collapseOffset: 100,
        collapsed: false,
        maxSize: 600,
        minSize: 200,
        pinned: true,
        resizable: true,
        side,
        size: undefined,
        widgets: [],
        maxWidgetCount: 2,
        splitterPercent: 50,
    };
}
/** @internal */
export function createVerticalPanelState(side, args) {
    return {
        ...createPanelState(side),
        ...args,
        side,
    };
}
/** @internal */
export function createHorizontalPanelState(side, args) {
    return {
        ...createPanelState(side),
        minSize: 100,
        span: true,
        ...args,
        side,
    };
}
/** @internal */
export function createPanelsState(args) {
    return {
        bottom: createHorizontalPanelState("bottom"),
        left: createVerticalPanelState("left"),
        right: createVerticalPanelState("right"),
        top: createHorizontalPanelState("top"),
        ...args,
    };
}
/** @internal */
export function updatePanelState(state, side, update) {
    return produce(state, (draft) => {
        const panel = draft.panels[side];
        update(panel);
    });
}
/** @internal */
export function getPanelPixelSizeFromSpec(side, appSize, panelSize) {
    if (typeof panelSize === "number") {
        return panelSize;
    }
    const fullSize = isHorizontalPanelSide(side) ? appSize.height : appSize.width;
    return (panelSize.percentage / 100) * fullSize;
}
/** @internal */
export function getPanelSize(preferredSizeSpec, side, minSizeSpec, maxSizeSpec, appSize) {
    if (preferredSizeSpec === undefined)
        return undefined;
    const maxSize = getPanelPixelSizeFromSpec(side, appSize, maxSizeSpec);
    const minSize = getPanelPixelSizeFromSpec(side, appSize, minSizeSpec);
    const preferredSize = getPanelPixelSizeFromSpec(side, appSize, preferredSizeSpec);
    return Math.min(Math.max(preferredSize, minSize), maxSize);
}
/** @internal */
export function addPanelWidget(state, side, id, tabs, widgetArgs) {
    return insertPanelWidget(state, side, id, tabs, Infinity, widgetArgs);
}
/** @internal */
export function insertPanelWidget(state, side, id, tabs, sectionIndex, widgetArgs) {
    const panel = state.panels[side];
    const maxWidgetCount = panel.maxWidgetCount;
    if (panel.widgets.length >= maxWidgetCount)
        // eslint-disable-next-line @typescript-eslint/no-deprecated
        throw new UiError(category, "Max widget count exceeded", undefined, () => ({
            maxWidgetCount,
        }));
    state = addWidgetState(state, id, tabs, widgetArgs);
    return produce(state, (draft) => {
        const widgets = draft.panels[side].widgets;
        widgets.splice(sectionIndex, 0, id);
    });
}
//# sourceMappingURL=PanelStateHelpers.js.map