"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeEventListener = exports.Element = void 0;
const NOOP_1 = require("../NOOP");
const evaluate_1 = require("../spec/evaluate");
const stringify_1 = require("../spec/stringify");
const component_1 = require("./component");
const change_1 = require("./event/change");
const custom_1 = require("./event/custom");
const input_1 = require("./event/input");
const keyboard_1 = require("./event/keyboard");
const paste_1 = require("./event/paste");
const click_1 = require("./event/pointer/click");
const pointerdown_1 = require("./event/pointer/pointerdown");
const pointerenter_1 = require("./event/pointer/pointerenter");
const pointerleave_1 = require("./event/pointer/pointerleave");
const pointermove_1 = require("./event/pointer/pointermove");
const pointerup_1 = require("./event/pointer/pointerup");
class Element extends component_1.Component {
    constructor() {
        super(...arguments);
        this.$preventLoad = false;
    }
    onConnected($unit) {
        const setRef = (name, { globalId }) => {
            const ref = $unit.$refGlobalObj({ globalId });
            // @ts-ignore
            this.setProp(name, ref);
        };
        const dropRef = (name) => {
            this.setProp(name, undefined);
        };
        const handler = {
            unit: (moment) => {
                const { specs, classes } = this.$system;
                const { event: event_event, data: event_data } = moment;
                if (event_event === 'set') {
                    const { name, data } = event_data;
                    // console.log('Element', 'set', name, data)
                    if (data !== undefined) {
                        const _data = (0, evaluate_1.evaluate)(data, specs, classes, (url) => {
                            const globalId = url.slice(7);
                            return this.$unit.$refGlobalObj({ globalId });
                        });
                        this.setProp(name, _data);
                    }
                    else {
                        this.setProp(name, undefined);
                    }
                }
            },
        };
        const element_listener = (moment) => {
            const { type } = moment;
            handler[type] && handler[type](moment);
        };
        const element_unlisten = this.$unit.$watch({ events: ['set', 'call' /**, 'ref_input' */] }, element_listener);
        this._element_unlisten = element_unlisten;
        if (!this.$preventLoad) {
            $unit.$read({}, (state) => {
                const { specs, classes } = this.$system;
                const _state = (0, evaluate_1.evaluate)(state, specs, classes, (url) => {
                    if (url.startsWith('unit://')) {
                        const globalId = url.slice(7);
                        return $unit.$refGlobalObj({ globalId });
                    }
                    else {
                        throw new Error('resolver not implemented');
                    }
                });
                for (const name in _state) {
                    const data = _state[name];
                    this.setProp(name, data);
                }
            });
        }
    }
    onDisconnected() {
        // console.log('Element', 'onDisconnected')
        this._element_unlisten();
    }
    set(name, data) {
        if (this.$connected) {
            const value = (0, stringify_1.stringify)(data);
            this.$unit.$set({ name, data: value }, NOOP_1.NOOP);
        }
    }
}
exports.Element = Element;
function makeEventListener(event, callback) {
    switch (event) {
        case 'click':
            return (0, click_1.makeClickListener)({ onClick: callback });
        case 'dbclick':
            return (0, click_1.makeClickListener)({ onDoubleClick: callback });
        case 'longpress':
            return (0, click_1.makeClickListener)({ onLongPress: callback });
        case 'longclick':
            return (0, click_1.makeClickListener)({ onLongClick: callback });
        case 'pointerdown':
            return (0, pointerdown_1.makePointerDownListener)(callback);
        case 'pointermove':
            return (0, pointermove_1.makePointerMoveListener)(callback);
        case 'pointerup':
            return (0, pointerup_1.makePointerUpListener)(callback);
        case 'pointerenter':
            return (0, pointerenter_1.makePointerEnterListener)(callback);
        case 'pointerleave':
            return (0, pointerleave_1.makePointerLeaveListener)(callback);
        case 'input':
            return (0, input_1.makeInputListener)(callback);
        case 'change':
            return (0, change_1.makeChangeListener)(callback);
        case 'paste':
            return (0, paste_1.makePasteListener)(callback);
        case 'keydown':
            return (0, keyboard_1.makeKeydownListener)(callback);
        case 'keyup':
            return (0, keyboard_1.makeKeyupListener)(callback);
        case 'keypress':
            return (0, keyboard_1.makeKeypressListener)(callback);
        default:
            return (0, custom_1.makeCustomListener)(event, callback);
    }
}
exports.makeEventListener = makeEventListener;
//# sourceMappingURL=element.js.map