"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Element_1 = require("../../../../Class/Element");
const bundleClass_1 = require("../../../../spec/bundleClass");
const _ids_1 = require("../../../_ids");
const _specs_1 = __importDefault(require("../../../_specs"));
const Parent_1 = __importDefault(require("../Parent"));
class Wrap extends Element_1.Element_ {
    constructor(system) {
        super({ i: ['component'], o: ['parent'] }, { output: { parent: { ref: true } } }, system, _ids_1.ID_WRAP);
        this._Container = (0, bundleClass_1.bundleClass)(Parent_1.default, {
            unit: { id: _ids_1.ID_PARENT },
        }, _specs_1.default);
        this._child_container = [];
        this._parent_container = [];
        this._parent_child_container = [];
        this.prependListener('set', (name, data) => {
            if (name === 'component') {
                this._Container = data;
                this._rewrap();
            }
        });
    }
    _rewrap() {
        for (let i = 0; i < this._child_container.length; i++) {
            const container = this._child_container[i];
            container.destroy();
            const new_container = new this._Container(this.__system);
            new_container.play();
            this._child_container[i] = new_container;
        }
        for (let i = 0; i < this._parent_container.length; i++) {
            const container = this._parent_container[i];
            container.destroy();
            const new_container = new this._Container(this.__system);
            new_container.play();
            this._parent_container[i] = new_container;
        }
        for (let i = 0; i < this._parent_child_container.length; i++) {
            const container = this._parent_child_container[i];
            container.destroy();
            const new_container = new this._Container(this.__system);
            new_container.play();
            this._parent_child_container[i] = new_container;
        }
    }
    appendChild(Class) {
        const container = new this._Container(this.__system);
        container.play();
        this._child_container.push(container);
        return super.appendChild(Class);
    }
    removeChild(at) {
        this._child_container.splice(at, 1);
        return super.removeChild(at);
    }
    registerParentRoot(component, subComponentId, slotName) {
        // console.log('Wrap', 'registerParentRoot')
        const container = new this._Container(this.__system);
        container.play();
        this._parent_container.push(container);
        return super.registerParentRoot(component, subComponentId, slotName);
    }
    unregisterParentRoot(component, subComponentId, emit = true) {
        const at = this._parent_container.indexOf(component);
        this._parent_container.splice(at, 1);
        return super.unregisterParentRoot(component, subComponentId, emit);
    }
    appendParentChild(component, slotName) {
        // console.log('Wrap', 'appendParentChild', component.constructor.name, slotName)
        const container = new this._Container(this.__system);
        container.play();
        this._parent_child_container.push(container);
        // container.appendChild(component) // TODO
        super.appendParentChild(container, slotName);
    }
    removeParentChild(component) {
        const at = this._parent_children.indexOf(component);
        if (at > -1) {
            const container = this._parent_child_container[at];
            // container.removeChild(0) // TODO
            super.removeParentChild(container);
        }
        else {
            throw new Error('parent child not found');
        }
    }
    refParentRootContainer(at) {
        return this._parent_container[at];
    }
    refChildContainer(at) {
        return this._child_container[at];
    }
    refParentChildContainer(at) {
        return this._parent_child_container[at];
    }
}
exports.default = Wrap;
//# sourceMappingURL=index.js.map