"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Component__ = void 0;
const isComponentEvent_1 = require("../../client/isComponentEvent");
const method_1 = require("../../component/method");
const MethodNotImplementedError_1 = require("../../exception/MethodNotImplementedError");
const Primitive_1 = require("../../Primitive");
const fromUnitBundle_1 = require("../../spec/fromUnitBundle");
const array_1 = require("../../util/array");
const clone_1 = require("../../util/clone");
class Component__ extends Primitive_1.Primitive {
    constructor({ i = [], o = [] }, opt = {}, system, id) {
        super({
            i,
            o,
        }, opt, system, id);
        this.__ = ['U', 'C', 'EE'];
        this._children = [];
        this._root = [];
        this._parent_root = [];
        this._parent_children = [];
        this._slot = {};
        this._animations = [];
        this._stopPropagation = {};
        this._stopImmediatePropagation = {};
        this._preventDefault = {};
        this._detachTo = {
            current: null,
        };
        this._attachDropTarget = [];
        this._attachText = [];
        this.addListener('reset', this._component_reset);
        this.addListener('play', this._component_play);
        this.addListener('pause', this._component_pause);
        this._slot = {
            default: this,
        };
    }
    detach(host, opt) {
        return (0, method_1.detach)(this, this._detachTo, host, opt);
    }
    isElement() {
        return true;
    }
    registerRoot(component, subComponentId, emit = true) {
        return (0, method_1.registerRoot)(this, this._root, component, subComponentId, emit);
    }
    unregisterRoot(component, subComponentId, emit = true) {
        return (0, method_1.unregisterRoot)(this, this._root, component, subComponentId, emit);
    }
    reorderRoot(component, to, emit = true) {
        return (0, method_1.reorderRoot)(this, this._root, component, to, emit);
    }
    registerParentRoot(component, subComponentId, slotName, at, emit = true) {
        return (0, method_1.registerParentRoot)(this, this._parent_root, subComponentId, component, slotName, at, emit);
    }
    unregisterParentRoot(component, subComponentId, emit) {
        return (0, method_1.unregisterParentRoot)(this, this._parent_root, subComponentId, component, emit);
    }
    reorderParentRoot(component, to) {
        return (0, method_1.reorderParentRoot)(this, this._parent_root, component, to, true);
    }
    appendParentChild(component, slotName) {
        return (0, method_1.appendParentChild)(this, this._parent_children, component, slotName, true);
    }
    removeParentChild(component) {
        return (0, method_1.removeParentChild)(this, this._parent_children, component, true);
    }
    appendChild(bundle, emit = true) {
        return (0, method_1.appendChild)(this, this._children, bundle, emit);
    }
    appendChildren(bundles) {
        return (0, method_1.appendChildren)(this, this._children, bundles);
    }
    insertChild(Bundle, at) {
        return (0, method_1.insertChild)(this, this._children, Bundle, at);
    }
    pushChild(Bundle) {
        return (0, method_1.pushChild)(this, this._children, Bundle)[0];
    }
    hasChild(at) {
        return (0, method_1.hasChild)(this, this._children, at);
    }
    removeChild(at, emit = true) {
        return (0, method_1.removeChild)(this, this._children, at);
    }
    pullChild(at) {
        throw (0, method_1.pullChild)(this, this._children, at);
    }
    refRoot(at) {
        return (0, method_1.refRoot)(this, this._children, at);
    }
    refChild(at) {
        return (0, method_1.refChild)(this, this._children, at);
    }
    refChildren() {
        return (0, method_1.refChildren)(this, this._children);
    }
    refSlot(slotName) {
        const slot = this._slot[slotName];
        if (slot) {
            return slot;
        }
        throw new MethodNotImplementedError_1.MethodNotImplementedError();
    }
    getAnimations() {
        return this._animations;
    }
    animate(keyframes, opt) {
        return (0, method_1.animate)(this, this._animations, keyframes, opt);
    }
    cancelAnimation(id) {
        return (0, method_1.cancelAnimation)(this, this._animations, id);
    }
    stopPropagation(name) {
        return (0, method_1.stopPropagation)(this, this._stopPropagation, name);
    }
    attachText(type, text) {
        return (0, method_1.attachText)(this, this._attachText, type, text);
    }
    attachDropTarget() {
        return (0, method_1.attachDropTarget)(this, this._attachDropTarget);
    }
    getSetup() {
        const setup = {
            animations: this._animations,
            events: this.eventNames().filter(isComponentEvent_1.isComponentEvent),
            stopPropagation: Object.keys(this._stopPropagation),
            stopImmediatePropagation: Object.keys(this._stopImmediatePropagation),
            preventDefault: Object.keys(this._preventDefault),
            detachTo: this._detachTo.current,
            attachText: this._attachText,
            attachDropTarget: this._attachDropTarget,
        };
        return setup;
    }
    _component_reset() {
        (0, array_1.forEach)(this._children, (u) => u.reset());
    }
    _component_play() {
        (0, array_1.forEach)(this._children, (u) => u.play());
    }
    _component_pause() {
        (0, array_1.forEach)(this._children, (u) => u.pause());
    }
    snapshotSelf() {
        return {
            ...super.snapshotSelf(),
            _children: this._children.map((child) => (0, clone_1.clone)(child.getUnitBundleSpec({ deep: true }))),
            _animations: (0, clone_1.clone)(this._animations),
            _stopPropagation: (0, clone_1.clone)(this._stopPropagation),
            _stopImmediatePropagation: (0, clone_1.clone)(this._stopImmediatePropagation),
            _preventDefault: (0, clone_1.clone)(this._preventDefault),
        };
    }
    restoreSelf(state) {
        const { _children = [], _animations = [], _stopPropagation = [], _stopImmediatePropagation = {}, _preventDefault = {}, ...rest } = state;
        super.restoreSelf(rest);
        while (this._children.length) {
            this.removeChild(0, false);
        }
        for (const child of _children) {
            const Bundle = (0, fromUnitBundle_1.fromUnitBundle)(child, this.__system.specs, this.__system.classes);
            this.appendChild(Bundle, false);
        }
        this._animations = _animations;
        this._stopPropagation = _stopPropagation;
        this._stopImmediatePropagation = _stopImmediatePropagation;
        this._preventDefault = _preventDefault;
    }
}
exports.Component__ = Component__;
//# sourceMappingURL=index.js.map