import { r as registerInstance, f as createEvent, h, d as getElement } from './index-cb74209c.js';
import { _ as _t } from './i18n-c66f020d.js';
import './_commonjsHelpers-4d8f6371.js';

const Form = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.glFormCancel = createEvent(this, "glFormCancel", 7);
    this.glFormFeatureChanged = createEvent(this, "glFormFeatureChanged", 7);
    this.glFormSubmit = createEvent(this, "glFormSubmit", 7);
    this.cancelText = undefined;
    this.feature = undefined;
    this.formId = `gl-form-${formId++}`;
    this.label = undefined;
    this.schema = undefined;
    this.submitText = undefined;
    this.translateText = false;
  }
  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 == undefined)
      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);
  }
  /**
   * Cancels and closes the form
   */
  async cancel() {
    this.glFormCancel.emit({
      formId: this.formId,
      feature: this.feature,
    });
  }
  /**
   * Submits the form and passes the feature that has been edited along to glFormSubmit
   */
  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 != undefined;
    if (label != undefined)
      page.label = label;
    if (this.submitText != undefined)
      page.submitText = this.submitText;
    if (this.cancelText != undefined)
      page.cancelText = this.cancelText;
    return page;
  }
  filter(items, formFacet) {
    return items
      .filter((item) => {
      let facets = item.facets || [];
      if (!facets.length && formFacet == undefined)
        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 = _t(item.label);
      if (item.options)
        result.options = this.filter(item.options, formFacet);
      return result;
    });
  }
  render() {
    return h("ion-nav", null);
  }
  get el() { return getElement(this); }
};
let formId = 0;

export { Form as gl_form };

//# sourceMappingURL=gl-form.entry.js.map