"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_STYLE = void 0;
const element_1 = require("../../../../client/element");
const parentElement_1 = require("../../../../client/platform/web/parentElement");
exports.DEFAULT_STYLE = {};
class Inherit extends element_1.Element {
    constructor(props, $system) {
        super(props, $system);
        this.$wrap = true;
        this._base = [];
        this._unlisten = [];
        this._registerChild = (child) => {
            let base = child.getRootBase();
            for (const parent_root of child.$parentRoot) {
                const parent_child_base = parent_root.getRootBase();
                base = [...base, ...parent_child_base];
            }
            for (const parent_child of child.$parentChildren) {
                const parent_child_base = parent_child.getRootBase();
                base = [...base, ...parent_child_base];
            }
            for (const leaf of base) {
                const [leaf_path, leaf_comp] = leaf;
                const unlisten = leaf_comp.interceptProp('style', (leaf_style) => {
                    const { style } = this.$props;
                    return Object.assign(Object.assign({}, style), leaf_style);
                });
                this._base.push(leaf_comp);
                this._unlisten.push(unlisten);
                leaf_comp.refreshProp('style');
            }
        };
        this._unregisterChild = (child) => {
            let base = child.getRootBase();
            for (const parent_child of child.$parentChildren) {
                const parent_child_base = parent_child.getRootBase();
                base = [...base, ...parent_child_base];
            }
            const base_length = base.length;
            const first_leaf = base[0];
            if (first_leaf) {
                const [_, first_leaf_comp] = first_leaf;
                const first_leaf_index = this._base.indexOf(first_leaf_comp);
                for (let i = 0; i < base_length; i++) {
                    const j = first_leaf_index + i;
                    const unlisten = this._unlisten[j];
                    const leaf_comp = this._base[j];
                    unlisten();
                    leaf_comp.refreshProp('style');
                }
                this._unlisten.splice(first_leaf_index, base.length);
                this._base.splice(first_leaf_index, base.length);
            }
        };
        const $element = (0, parentElement_1.default)($system);
        $element.className = 'default-style';
        this.$element = $element;
        this.$slot = {
            default: this,
        };
    }
    domAppendParentChildAt(child, slotName, at, _at) {
        this._registerChild(child);
        super.domAppendParentChildAt(child, slotName, at, _at);
    }
    domAppendParentRoot(child, slotName, at) {
        this._registerChild(child);
        super.domAppendParentRoot(child, slotName, at);
    }
    domRemoveParentRootAt(component, slotName, at, _at) {
        this._unregisterChild(component);
        super.domRemoveParentRootAt(component, slotName, at, _at);
    }
    domRemoveParentChildAt(child, slotName, at, _at) {
        this._unregisterChild(child);
        super.domRemoveParentChildAt(child, slotName, at, _at);
    }
    onPropChanged(name, style) {
        // if (name === 'style') {
        for (const leaf_comp of this._base) {
            leaf_comp.refreshProp('style');
        }
        // }
    }
}
exports.default = Inherit;
//# sourceMappingURL=Component.js.map