"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module ToolSettings
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.eqlOverflown = exports.useToolSettingsEntry = exports.useOverflow = exports.getOverflown = exports.getChildKey = exports.DockedToolSettings = exports.onOverflowLabelAndEditorResize = void 0;
require("./Docked.scss");
const core_bentley_1 = require("@itwin/core-bentley");
const core_react_1 = require("@itwin/core-react");
const classnames_1 = require("classnames");
const React = require("react");
const react_transition_group_1 = require("react-transition-group");
const NineZone_1 = require("../base/NineZone");
const Handle_1 = require("./Handle");
const Overflow_1 = require("./Overflow");
const Panel_1 = require("./Panel");
/** @internal */
function onOverflowLabelAndEditorResize() { }
exports.onOverflowLabelAndEditorResize = onOverflowLabelAndEditorResize;
/** This component takes a DockedToolSetting "wrapper" component and extract only the label and editor components from it */
function OverflowLabelAndEditor({ wrapper }) {
    (0, core_bentley_1.assert)(React.isValidElement(wrapper));
    const entryValue = React.useMemo(() => ({
        isOverflown: true,
        onResize: onOverflowLabelAndEditorResize,
    }), []);
    const wrapperChildren = wrapper.props.children;
    return (React.createElement(React.Fragment, null,
        React.createElement(DockedToolSettingsEntryContext.Provider, { value: entryValue }, wrapperChildren &&
            React.Children.map(wrapperChildren, (child) => child))));
}
/** Component that displays tool settings as a bar across the top of the content view.
 * @internal
 */
function DockedToolSettings(props) {
    const [open, setOpen] = React.useState(false);
    const animateToolSettings = React.useContext(NineZone_1.AnimateDockedToolSettingsContext);
    const ref = React.useRef(null);
    const width = React.useRef(undefined);
    const handleWidth = React.useRef(undefined);
    const [overflown, handleContainerResize, handleOverflowResize, getOnEntryResize, handleGapResize,] = useOverflow(props.children);
    const onResize = React.useCallback(() => {
        let gapSize = 0;
        if (ref.current) {
            gapSize = getGapVariable(ref.current);
            handleGapResize(gapSize);
        }
        width.current !== undefined &&
            handleWidth.current !== undefined &&
            handleContainerResize(width.current - handleWidth.current - gapSize);
    }, [handleContainerResize, handleGapResize]);
    const handleHandleResize = React.useCallback((w) => {
        handleWidth.current = w;
        onResize();
    }, [onResize]);
    const handleResize = React.useCallback((w) => {
        width.current = w;
        onResize();
    }, [onResize]);
    const resizeObserverRef = (0, core_react_1.useResizeObserver)(handleResize);
    const onOverflowClick = React.useCallback(() => {
        setOpen((prev) => !prev);
    }, []);
    const handleOnClose = React.useCallback(() => {
        setOpen(false);
    }, []);
    const targetRef = React.useRef(null);
    const refs = (0, core_react_1.useRefs)(ref, resizeObserverRef);
    const children = React.useMemo(() => React.Children.toArray(props.children), [props.children]);
    const dockedChildren = children.reduce((acc, child, index) => {
        const key = getChildKey(child, index);
        if (!overflown || overflown.indexOf(key) < 0) {
            acc.push([key, child]);
        }
        return acc;
    }, []);
    const overflownChildren = overflown
        ? children.reduce((acc, child, index) => {
            const key = getChildKey(child, index);
            if (overflown.indexOf(key) >= 0) {
                acc.push([key, child]);
            }
            return acc;
        }, [])
        : [];
    const PanelContainer = props.panelContainer
        ? props.panelContainer
        : DefaultPanelContainer;
    const className = (0, classnames_1.default)("uifw-toolSettings-docked", props.className);
    return (React.createElement("div", { "data-toolsettings-provider": props.itemId, className: className, ref: refs, style: props.style },
        React.createElement(Handle_1.DockedToolSettingsHandle, { onResize: handleHandleResize }),
        dockedChildren.map(([key, child]) => {
            return (React.createElement(react_transition_group_1.TransitionGroup, { appear: animateToolSettings, enter: animateToolSettings, key: key },
                React.createElement(react_transition_group_1.CSSTransition, { in: animateToolSettings, timeout: 400, classNames: {
                        appear: "toolsettings-appear",
                        enter: "toolsettings-appear",
                        appearActive: "toolsettings-appear-active",
                        enterActive: "toolsettings-appear-active",
                    } },
                    React.createElement(DockedToolSettingsEntry, { key: key, entryKey: key, getOnResize: getOnEntryResize }, child))));
        }),
        (!overflown || overflown.length > 0) && (React.createElement(React.Fragment, null,
            React.createElement(Overflow_1.DockedToolSettingsOverflow, { onClick: onOverflowClick, onResize: handleOverflowResize, ref: targetRef }),
            overflownChildren.length > 0 && open && targetRef.current && (React.createElement(Panel_1.ToolSettingsOverflowPanel, { onClose: handleOnClose, open: true, target: targetRef.current },
                React.createElement(PanelContainer, null, overflownChildren.map(([key, child]) => {
                    return React.createElement(OverflowLabelAndEditor, { key: key, wrapper: child });
                }))))))));
}
exports.DockedToolSettings = DockedToolSettings;
function DockedToolSettingsEntry({ children, entryKey, getOnResize, }) {
    const onResize = React.useMemo(() => getOnResize(entryKey), [getOnResize, entryKey]);
    const entry = React.useMemo(() => ({
        isOverflown: false,
        onResize,
    }), [onResize]);
    return (React.createElement(DockedToolSettingsEntryContext.Provider, { value: entry }, children));
}
/** Returns key of a child. Must be used along with React.Children.toArray to preserve the semantics of children.
 * @internal
 */
function getChildKey(child, index) {
    if (React.isValidElement(child) && child.key !== null) {
        return child.key.toString();
    }
    return index.toString();
}
exports.getChildKey = getChildKey;
/** Returns a subset of docked entry keys that exceed given width and should be overflown.
 * @internal
 */
function getOverflown(width, docked, overflowWidth, activeIndex, gapSize = 0) {
    let settingsWidth = 0;
    // Avoid adding gap to first item.
    let firstItem = true;
    const getGapSize = () => {
        if (firstItem) {
            firstItem = false;
            return 0;
        }
        return gapSize;
    };
    // Make sure active item is always visible.
    if (activeIndex !== undefined && docked.length > activeIndex) {
        const activeWidth = docked[activeIndex];
        settingsWidth += activeWidth[1] + getGapSize();
    }
    let i = 0;
    for (; i < docked.length; i++) {
        // Skip active item.
        if (i === activeIndex)
            continue;
        const itemWidth = docked[i][1];
        const newSettingsWidth = settingsWidth + itemWidth + getGapSize();
        // Items overflow, add an overflow button.
        if (newSettingsWidth > width) {
            settingsWidth += overflowWidth + getGapSize();
            break;
        }
        // Items fit, add an item.
        settingsWidth = newSettingsWidth;
    }
    // Remove items until they fit.
    let j = i;
    if (j < docked.length) {
        for (; j > 0; j--) {
            if (j === activeIndex)
                continue;
            if (settingsWidth <= width)
                break;
            const itemWidth = docked[j][1];
            settingsWidth -= itemWidth + gapSize;
        }
    }
    const overflown = new Array();
    for (i = j; i < docked.length; i++) {
        if (i === activeIndex)
            continue;
        overflown.push(docked[i][0]);
    }
    return overflown;
}
exports.getOverflown = getOverflown;
/** Hook that returns a list of overflown children keys.
 * @internal
 */
function useOverflow(children, activeChildIndex) {
    const [overflown, setOverflown] = React.useState();
    const entryWidths = React.useRef(new Map());
    const width = React.useRef(undefined);
    const overflowWidth = React.useRef(undefined);
    const gapSize = React.useRef(0);
    const calculateOverflow = React.useCallback(() => {
        const widths = verifiedMapEntries(entryWidths.current);
        if (width.current === undefined ||
            widths === undefined ||
            overflowWidth.current === undefined) {
            setOverflown(undefined);
            return;
        }
        // Calculate overflow.
        const newOverflown = getOverflown(width.current, [...widths.entries()], overflowWidth.current, activeChildIndex, gapSize.current);
        setOverflown((prevOverflown) => {
            return eqlOverflown(prevOverflown, newOverflown)
                ? prevOverflown
                : newOverflown;
        });
    }, [activeChildIndex]);
    React.useLayoutEffect(() => {
        const newEntryWidths = new Map();
        const array = React.Children.toArray(children);
        for (let i = 0; i < array.length; i++) {
            const child = array[i];
            const key = getChildKey(child, i);
            const lastW = entryWidths.current.get(key);
            newEntryWidths.set(key, lastW);
        }
        entryWidths.current = newEntryWidths;
        calculateOverflow();
    }, [children, calculateOverflow]);
    const handleContainerResize = React.useCallback((w) => {
        const calculate = width.current !== w;
        width.current = w;
        calculate && calculateOverflow();
    }, [calculateOverflow]);
    const handleOverflowResize = React.useCallback((w) => {
        const calculate = overflowWidth.current !== w;
        overflowWidth.current = w;
        calculate && calculateOverflow();
    }, [calculateOverflow]);
    const handleEntryResize = React.useCallback((key) => (w) => {
        const oldW = entryWidths.current.get(key);
        if (oldW === w)
            return;
        entryWidths.current.set(key, w);
        calculateOverflow();
    }, [calculateOverflow]);
    const handleGapResize = React.useCallback((w) => {
        if (gapSize.current === w)
            return;
        gapSize.current = w;
        calculateOverflow();
    }, [calculateOverflow]);
    return [
        overflown,
        handleContainerResize,
        handleOverflowResize,
        handleEntryResize,
        handleGapResize,
    ];
}
exports.useOverflow = useOverflow;
const DockedToolSettingsEntryContext = React.createContext(null);
DockedToolSettingsEntryContext.displayName =
    "nz:DockedToolSettingsEntryContext";
/** @internal */
function useToolSettingsEntry() {
    return React.useContext(DockedToolSettingsEntryContext);
}
exports.useToolSettingsEntry = useToolSettingsEntry;
function verifiedMapEntries(map) {
    for (const [, val] of map) {
        if (val === undefined)
            return undefined;
    }
    return map;
}
function DefaultPanelContainer(props) {
    return (React.createElement("div", { className: "uifw-toolSettings-docked_container" }, props.children));
}
/** @internal */
function eqlOverflown(prev, value) {
    if (!prev)
        return false;
    if (prev.length !== value.length)
        return false;
    for (let i = 0; i < prev.length; i++) {
        const p = prev[i];
        const v = value[i];
        if (p !== v)
            return false;
    }
    return true;
}
exports.eqlOverflown = eqlOverflown;
function getGapVariable(htmlElement) {
    // eslint-disable-next-line deprecation/deprecation
    const gap = (0, core_react_1.getCssVariableAsNumber)("gap", htmlElement);
    if (isNaN(gap))
        return 0;
    return gap;
}
//# sourceMappingURL=Docked.js.map