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());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../../core/AtomBinder", "../../core/AtomBridge", "../../core/AtomComponent", "../../di/TypeKey", "../../services/NavigationService", "../../web/styles/AtomStyle", "../../web/styles/AtomStyleSheet"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.AtomXFControl = void 0;
    const AtomBinder_1 = require("../../core/AtomBinder");
    const AtomBridge_1 = require("../../core/AtomBridge");
    const AtomComponent_1 = require("../../core/AtomComponent");
    const TypeKey_1 = require("../../di/TypeKey");
    const NavigationService_1 = require("../../services/NavigationService");
    const AtomStyle_1 = require("../../web/styles/AtomStyle");
    const AtomStyleSheet_1 = require("../../web/styles/AtomStyleSheet");
    UMD.defaultApp = "@web-atoms/core/dist/xf/XFApp";
    UMD.viewPrefix = "xf";
    AtomBridge_1.AtomBridge.platform = "xf";
    const defaultStyleSheets = {};
    class AtomXFControl extends AtomComponent_1.AtomComponent {
        get controlStyle() {
            if (this.mControlStyle === undefined) {
                const key = TypeKey_1.TypeKey.get(this.defaultControlStyle || this.constructor);
                this.mControlStyle = defaultStyleSheets[key];
                if (this.mControlStyle) {
                    return this.mControlStyle;
                }
                if (this.defaultControlStyle) {
                    this.mControlStyle = defaultStyleSheets[key] ||
                        (defaultStyleSheets[key] = this.theme.createNamedStyle(this.defaultControlStyle, key, 0));
                }
                this.mControlStyle = this.mControlStyle || null;
            }
            return this.mControlStyle;
        }
        set controlStyle(v) {
            if (v instanceof AtomStyle_1.AtomStyle) {
                this.mControlStyle = v;
            }
            else {
                const key = TypeKey_1.TypeKey.get(v);
                this.mControlStyle = defaultStyleSheets[key] ||
                    (defaultStyleSheets[key] = this.theme.createNamedStyle(v, key, 0));
            }
            AtomBinder_1.AtomBinder.refreshValue(this, "controlStyle");
            // this.invalidate();
        }
        get theme() {
            return this.mTheme ||
                this.mCachedTheme ||
                (this.mCachedTheme = (this.parent ? this.parent.theme : this.app.resolve(AtomStyleSheet_1.AtomStyleSheet, false, null)));
        }
        get parent() {
            return AtomBridge_1.AtomBridge.instance.atomParent(this.element, true);
        }
        atomParent(e) {
            return AtomBridge_1.AtomBridge.instance.atomParent(e, false);
        }
        append(element) {
            this.element.appendChild(element.element || element);
            return this;
        }
        dispose(e) {
            const el = this.element;
            super.dispose(e);
            AtomBridge_1.AtomBridge.instance.dispose(el);
        }
        invokeEvent(event) {
            AtomBridge_1.AtomBridge.instance.invokeEvent(this.element, event.type, event);
        }
        staticResource(name) {
            return AtomBridge_1.AtomBridge.instance.getStaticResource(this.element, name);
        }
        loadXaml(text) {
            AtomBridge_1.AtomBridge.instance.loadXaml(this.element, text);
        }
        setElementValue(element, name, value) {
            if (/^event/.test(name)) {
                this.bindEvent(element, name.substr(5), () => __awaiter(this, void 0, void 0, function* () {
                    try {
                        const p = value();
                        if (p) {
                            yield p;
                        }
                    }
                    catch (e) {
                        if (/canceled|cancelled/i.test(e)) {
                            return;
                        }
                        const nav = this.app.resolve(NavigationService_1.NavigationService);
                        nav.alert(e, "Error").catch(() => {
                            // nothing...
                        });
                    }
                }));
                return;
            }
            if (/^(class|styleClass)$/i.test(name)) {
                let classes;
                if (typeof value === "object") {
                    classes = [];
                    for (const key in value) {
                        if (value.hasOwnProperty(key)) {
                            const e1 = value[key];
                            if (e1) {
                                classes.push(key);
                            }
                        }
                    }
                }
                else {
                    classes = value.toString().split(" ");
                }
                value = classes.join(",");
            }
            AtomBridge_1.AtomBridge.instance.setValue(element, name, value);
        }
    }
    exports.AtomXFControl = AtomXFControl;
    bridge.controlFactory = AtomXFControl;
    global.CustomEvent = function (type, { detail }) {
        this.type = type;
        this.detail = detail;
    };
});
//# sourceMappingURL=AtomXFControl.js.map