import _ from 'lodash';
import '../../../../globals.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import Vector2Property from '../../../../dot/js/Vector2Property.js';
import { Node, scenery, Sizable } from '../../imports.js';
export const LAYOUT_NODE_OPTION_KEYS = ['resize', 'layoutOrigin'];
export default class LayoutNode extends Sizable(Node) {
    constructor(providedOptions) {
        super(providedOptions);
        this.layoutOriginProperty = new Vector2Property(Vector2.ZERO);
    }
    linkLayoutBounds() {
        // Adjust the localBounds to be the laid-out area (defined by the constraint)
        this._constraint.layoutBoundsProperty.link(layoutBounds => {
            this.localBounds = layoutBounds;
        });
    }
    setExcludeInvisibleChildrenFromBounds(excludeInvisibleChildrenFromBounds) {
        super.setExcludeInvisibleChildrenFromBounds(excludeInvisibleChildrenFromBounds);
        this._constraint.excludeInvisible = excludeInvisibleChildrenFromBounds;
    }
    setChildren(children) {
        // If the layout is already locked, we need to bail and only call Node's setChildren. This is fine, our layout will
        // be handled once whatever locked us unlocks (so we don't have to override to handle layout or locking/unlocking.
        if (this.constraint.isLocked) {
            return super.setChildren(children);
        }
        const oldChildren = this.getChildren(); // defensive copy
        // Lock layout while the children are removed and added
        this.constraint.lock();
        super.setChildren(children);
        this.constraint.unlock();
        // Determine if the children array has changed. We'll gain a performance benefit by not triggering layout when
        // the children haven't changed.
        if (!_.isEqual(oldChildren, children)) {
            this.constraint.updateLayoutAutomatically();
        }
        return this;
    }
    /**
     * Manually run the layout (for instance, if resize:false is currently set, or if there is other hackery going on).
     */
    updateLayout() {
        this._constraint.updateLayout();
    }
    get resize() {
        return this._constraint.enabled;
    }
    set resize(value) {
        this._constraint.enabled = value;
    }
    get layoutOrigin() {
        return this.layoutOriginProperty.value;
    }
    set layoutOrigin(value) {
        this.layoutOriginProperty.value = value;
    }
    /**
     * Manual access to the constraint. This is needed by subtypes to lock/unlock or force layout updates, but may also
     * be needed to read layout information out (for overlays, GridBackgroundNode, etc.)
     */
    get constraint() {
        return this._constraint;
    }
    /**
     * Releases references
     */
    dispose() {
        this._constraint.dispose();
        super.dispose();
    }
}
scenery.register('LayoutNode', LayoutNode);
//# sourceMappingURL=LayoutNode.js.map