'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const __chunk_1 = require('./chunk-91f43df3.js');
const __chunk_2 = require('./chunk-a2a6832b.js');

class Form {
    constructor(hostRef) {
        __chunk_1.registerInstance(this, hostRef);
        this.formId = `gl-form-${formId++}`;
        this.translateText = false;
        this.glFormCancel = __chunk_1.createEvent(this, "glFormCancel", 7);
        this.glFormFeatureChanged = __chunk_1.createEvent(this, "glFormFeatureChanged", 7);
        this.glFormSubmit = __chunk_1.createEvent(this, "glFormSubmit", 7);
    }
    async componentWillLoad() {
        let res = await fetch(this.schema);
        this._schema = await res.json();
    }
    componentDidLoad() {
        let page = this.createFormPage(this.label);
        this.el.querySelector('ion-nav').setRoot(page);
    }
    setValue(e) {
        if (!this.feature)
            return;
        this.feature.properties = this.feature.properties || {};
        this.feature.properties[e.detail.field.attribute] = e.detail.value;
        this.glFormFeatureChanged.emit({
            formId: this.formId,
            feature: this.feature
        });
    }
    handleFacet(e) {
        let page = this.createFormPage(e.detail.label, e.detail.value);
        this.el.querySelector('ion-nav').push(page);
    }
    async cancel() {
        this.glFormCancel.emit({
            formId: this.formId,
            feature: this.feature
        });
    }
    async submit() {
        this.glFormSubmit.emit({
            formId: this.formId,
            feature: this.feature
        });
    }
    createFormPage(label, formFacet) {
        let page = document.createElement('gl-form-page');
        page.formFacet = formFacet;
        page.facets = this.filter(this._schema.facets || [], formFacet);
        page.fields = this.filter(this._schema.fields || [], formFacet);
        page.root = !formFacet;
        if (label)
            page.label = label;
        if (this.submitText)
            page.submitText = this.submitText;
        if (this.cancelText)
            page.cancelText = this.cancelText;
        return page;
    }
    filter(items, formFacet) {
        return items.filter((item) => {
            let facets = item.facets || [];
            if (!facets.length && !formFacet)
                return true;
            for (let facet of facets) {
                let escaped = facet.replace(/[.?+^$[\]\\(){}|-]/g, '\\$&');
                let re = new RegExp('^' + escaped.split('*').join('.*') + '$');
                if (re.test(formFacet))
                    return true;
            }
            return false;
        }).map((item) => {
            let result = Object.assign({}, item);
            if (this.translateText)
                result.label = __chunk_2._t(item.label);
            if (item.options)
                result.options = this.filter(item.options, formFacet);
            return result;
        });
    }
    render() {
        return (__chunk_1.h("ion-nav", null));
    }
    get el() { return __chunk_1.getElement(this); }
}
let formId = 0;

exports.gl_form = Form;
