'use strict';

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

const index = require('./index-b8c5f2d4.js');
const i18n = require('./i18n-92762f48.js');
require('./_commonjsHelpers-dcc4cf71.js');

const attributeValuesCss = "gl-attribute-values ion-note{font-size:14px;line-height:14px;padding-top:16px;text-align:right}";

const AttributeValues = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.options = [];
    this.attributeOptions = undefined;
    this.features = undefined;
    this.header = i18n._t("webmapgl.attribute-values.header");
    this.headerAttribute = undefined;
    this.labelPosition = undefined;
    this.limit = 1;
    this.valueMaxWidth = "200px";
  }
  componentWillLoad() {
    this.parseAttributeOptions();
  }
  /**
   * Converts a format attribute option from a string to the json object we can pass into toLocaleString.
   * If the format is one of the three possible string values, create a specific options object for that.
   */
  parseFormat(format) {
    if (typeof format !== "string")
      return format;
    let parsedFormat;
    try {
      parsedFormat = JSON.parse(format);
    }
    catch (e) {
      if (/SyntaxError/.test(e)) {
        if (["currency", "decimal", "percent"].indexOf(format) !== -1) {
          parsedFormat = {
            style: format,
          };
          if (format === "currency")
            parsedFormat["currency"] = "USD";
        }
      }
      else {
        throw e;
      }
    }
    return parsedFormat;
  }
  parseAttributeOptions() {
    this.options =
      typeof this.attributeOptions === "string"
        ? JSON.parse(this.attributeOptions)
        : this.attributeOptions;
  }
  getItemsForFeature(feature, options) {
    return options
      .filter((attr) => attr.name !== this.headerAttribute &&
      feature.properties[attr.name] != undefined)
      .map((attr) => (index.h("ion-item", null, index.h("ion-label", { position: this.labelPosition }, attr.alias || attr.name), index.h("ion-note", { slot: this.labelPosition != "stacked" ? "end" : null, "text-wrap": true, style: { "max-width": this.valueMaxWidth } }, attr.format
      ? feature.properties[attr.name].toLocaleString({}, this.parseFormat(attr.format))
      : feature.properties[attr.name]))));
  }
  generateOptions(feature) {
    let opts = [];
    for (let prop in feature.properties) {
      opts.push({
        name: prop,
      });
    }
    return opts;
  }
  render() {
    if (this.features == undefined || this.features.length === 0)
      return null;
    let items = [];
    for (let feature of this.features.slice(0, this.limit)) {
      let header = this.headerAttribute != undefined
        ? feature.properties[this.headerAttribute]
        : this.header;
      items.push(index.h("ion-item-divider", null, index.h("ion-label", null, header)));
      let featureOpts = this.options || this.generateOptions(feature);
      Array.prototype.push.apply(items, this.getItemsForFeature(feature, featureOpts));
    }
    return index.h("ion-list", null, items);
  }
  get el() { return index.getElement(this); }
  static get watchers() { return {
    "attributeOptions": ["parseAttributeOptions"]
  }; }
};
AttributeValues.style = attributeValuesCss;

exports.gl_attribute_values = AttributeValues;

//# sourceMappingURL=gl-attribute-values.cjs.entry.js.map