"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const Functional_1 = require("../../../Class/Functional");
const addListener_1 = require("../../../client/addListener");
const context_1 = require("../../../client/context");
const custom_1 = require("../../../client/event/custom");
const resize_1 = require("../../../client/event/resize");
const ObjectWaiter_1 = require("../../../ObjectWaiter");
const globalComponent_1 = require("../../globalComponent");
const _ids_1 = require("../../_ids");
class _Context extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['element'],
            o: [],
        }, {
            input: {
                element: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_CONTEXT);
        this._context_waiter = new ObjectWaiter_1.ObjectWaiter();
    }
    subscribe(path, key, listener) {
        throw new Error('Method not implemented.');
    }
    f({ element }) {
        const globalId = element.getGlobalId();
        const _mount = ($context) => {
            // console.log('Context', '_mount')
            this._context = $context;
            const unlisten_context = (0, addListener_1.addListener)(this._context, (0, resize_1.makeResizeListener)(({ width, height }) => {
                this.emit('_resize', { width, height });
            }));
            this._unlisten_context = unlisten_context;
            this._context_waiter.set($context);
        };
        const _unmount = () => {
            // console.log('Context', '_unmount')
            if (this._unlisten_context) {
                this._unlisten_context();
                this._unlisten_context = undefined;
            }
            this._context = null;
        };
        const component = this.__system.getRemoteComponent(globalId);
        if (component) {
            if (component.$mounted) {
                _mount(component.$context);
            }
            else {
                //
            }
        }
        else {
            //
        }
        const unlisten = (0, globalComponent_1.listenGlobalComponent)(this.__system, globalId, (_component) => {
            this._unlisten = _component.addEventListeners([
                (0, custom_1.makeCustomListener)('mount', () => {
                    const { $context } = _component;
                    _mount($context);
                }),
                (0, custom_1.makeCustomListener)('unmount', () => {
                    _unmount();
                }),
            ]);
            if (_component.$mounted) {
                _mount(_component.$context);
            }
        });
        this._unlisten = unlisten;
    }
    d() {
        if (this._unlisten) {
            this._unlisten();
            this._unlisten = undefined;
        }
        if (this._unlisten_context) {
            this._unlisten_context();
            this._unlisten_context = undefined;
        }
        this._context = undefined;
    }
    get(name) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this._context_waiter.once();
            switch (name) {
                case 'color': {
                    const { $color } = this._context;
                    return $color;
                }
                case 'width': {
                    const { $width } = this._context;
                    return $width;
                }
                case 'height': {
                    const { $height } = this._context;
                    return $height;
                }
                default: {
                    throw new Error('property not defined');
                }
            }
        });
    }
    set(name, data) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this._context_waiter.once();
            switch (name) {
                case 'color': {
                    return (0, context_1.setColor)(this._context, data);
                }
                case 'width': {
                    throw new Error('width is read only');
                }
                case 'height': {
                    throw new Error('height is read only');
                }
                default: {
                    throw new Error('property not defined');
                }
            }
        });
    }
    delete(name) {
        throw new Error('Method not implemented.');
    }
    pathSet(path, name, data) {
        throw new Error('Method not implemented.');
    }
    pathGet(path, name) {
        throw new Error('Method not implemented.');
    }
    pathDelete(path, name) {
        throw new Error('Method not implemented.');
    }
    keys() {
        throw new Error('Method not implemented.');
    }
    hasKey(name) {
        throw new Error('Method not implemented.');
    }
}
exports.default = _Context;
//# sourceMappingURL=index.js.map