<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">define(["require", "exports", "esri/core/reactiveUtils", "./debounce", "./esriWidgetUtils"], function (require, exports, reactiveUtils_1, debounce_1, esriWidgetUtils_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.handleBatchWidgetPositions = exports.assertWidgetOrdering = void 0;
    const positionManagerBatch = {};
    const lastPositionValue = new Map(); // positionKey =&gt; serializedPosition
    const serializePosition = ({ position, index } = { position: "manual", index: 0 }) =&gt; `${position}-${index}`;
    /**
     * Deals with issue where widgets load at different times and the positions in the
     * Map UI end up not matching what's prescribed in the Position Manager.
     * https://devtopia.esri.com/WebGIS/arcgis-template-configuration/issues/3105
     *
     * Use debug === true to find the proper positionKeyLookup values
     * (note widgets must be present in the UI, which means turned on in the config)
     * @param view
     * @param config
     * @param positionKeyLookup
     * @param debug
     */
    function assertWidgetOrdering(view, config, positionKeyLookup, debug) {
        (0, reactiveUtils_1.when)(() =&gt; view?.ready === true, () =&gt; {
            view.when(() =&gt; {
                const watchWidgetOrdering = (quadrant) =&gt; {
                    let firstAssertion = true;
                    (0, reactiveUtils_1.watch)(() =&gt; config.updateCount, () =&gt; {
                        let widgets = view.ui.getComponents(quadrant);
                        let positionsHaveChanged = widgets.some((el) =&gt; {
                            // check last config value for positionValue
                            const positionKey = getPositionKey(el.id, positionKeyLookup);
                            const positionValue = config[positionKey];
                            if (positionValue != null &amp;&amp; positionValue?.position !== quadrant) {
                                // major side effect - but if the app code doesn't handle moving between quadrants, then this is necessary
                                view.ui.move(el, positionValue);
                            }
                            const serializedPosition = serializePosition(positionValue);
                            return lastPositionValue.get(positionKey) !== serializedPosition;
                        });
                        if (positionsHaveChanged || firstAssertion) {
                            firstAssertion = false;
                            setTimeout(() =&gt; {
                                widgets = view.ui.getComponents(quadrant);
                                const sortedPositionList = widgets
                                    .map((el) =&gt; {
                                    let positionKey = getPositionKey(el.id, positionKeyLookup);
                                    let positionValue = config[positionKey];
                                    lastPositionValue.set(positionKey, serializePosition(positionValue));
                                    if (debug) {
                                        console.log(`id: ${el.id}, ====&gt; looking in config for: ` +
                                            `%c${positionKey}`, "font-weight: bold; color: blue;");
                                        if (positionValue == null) {
                                            console.log(`%c${positionKey} doesn't seem to be a key. Check the ConfigSettings to find what it should be, and update positionKeyLookup with necessary override.`, "font-weight: bold; color: red;");
                                        }
                                    }
                                    return [el.id, positionValue];
                                })
                                    .sort((a, b) =&gt; {
                                    return a[1]?.index - b[1]?.index;
                                });
                                sortedPositionList.forEach((sortedPair, index) =&gt; {
                                    const [id, positionValue] = sortedPair;
                                    if (positionValue) {
                                        positionValue.index = index; // override to directly match sorted list
                                        const el = widgets.find((elem) =&gt; elem.id === id);
                                        view.ui.move({ component: el, ...positionValue });
                                        updateExpandGroup(el, positionValue);
                                    }
                                });
                            }, 200);
                        }
                    }, { initial: true });
                };
                watchWidgetOrdering("top-left");
                watchWidgetOrdering("top-right");
                watchWidgetOrdering("bottom-left");
                watchWidgetOrdering("bottom-right");
            });
        }, {
            initial: true
        });
    }
    exports.assertWidgetOrdering = assertWidgetOrdering;
    function getPositionKey(id, positionKeyLookup) {
        const correctId = positionKeyLookup.get(id) != null ? positionKeyLookup.get(id) : id;
        return `${correctId}Position`;
    }
    /**
     * This function handles the batch positioning of widgets in a view. Replace the `view.ui.move` method with this function to batch the positioning of widgets.
     * @param {__esri.MapView | __esri.SceneView} view - The view in which the widgets are located.
     * @param {__esri.Widget | HTMLElement | string} element - The widget, HTML element, or widget id to be positioned.
     * @param {IWidgetPosition | __esri.UIPosition} uiPosition - The new position for the widget.
     * @param {number} [viewInstance=0] - The instance of the view. This is useful when you have multiple views and want to specify in which one the widget should be positioned.
     */
    function handleBatchWidgetPositions(view, element, uiPosition, viewInstance = 0) {
        if (!isValidInput(view, element, uiPosition))
            return;
        if (positionManagerBatch[viewInstance] == null) {
            positionManagerBatch[viewInstance] = {};
        }
        const widget = getWidget(view, element);
        if (widget) {
            const position = extractPositionAndIndex(uiPosition);
            updatePositionManager(viewInstance, widget, position);
            debouncePositionUpdate(view, viewInstance, position);
        }
    }
    exports.handleBatchWidgetPositions = handleBatchWidgetPositions;
    function updatePositionManager(viewInstance, widget, uiPosition) {
        const positionManager = positionManagerBatch[viewInstance];
        positionManager[widget.id] = { uiPosition, widget };
    }
    function debouncePositionUpdate(view, viewInstance, uiPosition) {
        const { position } = uiPosition;
        (0, debounce_1.debounce)(() =&gt; {
            const positionManager = positionManagerBatch[viewInstance];
            const components = Object.values(positionManager).filter((el) =&gt; el.uiPosition.position === position);
            components.sort((a, b) =&gt; a.uiPosition.index - b.uiPosition.index);
            components.forEach((component, index) =&gt; {
                const position = {
                    index,
                    position: component.uiPosition.position,
                };
                view.ui.move(component.widget, position);
            });
        }, 200, false, viewInstance, `widgetPositionBatch-${viewInstance}-${position}`);
    }
    function isValidInput(view, element, uiPosition) {
        return Boolean(view &amp;&amp; element &amp;&amp; uiPosition);
    }
    function extractPositionAndIndex(uiPosition) {
        return typeof uiPosition === "string"
            ? { position: uiPosition }
            : { position: uiPosition.position, index: uiPosition.index };
    }
    function getWidget(view, element) {
        if (typeof element === "string") {
            return view.ui.find(element);
        }
        else {
            return element;
        }
    }
    function updateExpandGroup(el, positionLookup) {
        if (el?.declaredClass === "esri.widgets.Expand") {
            const group = (0, esriWidgetUtils_1.getPosition)(positionLookup);
            el.group = group;
        }
    }
});
</pre></body></html>