'use strict';

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

const index$1 = require('./index-df541290.js');
const index$2 = require('./index-c12b8888.js');
const index = require('./index-b7493032.js');
require('./index-0b6f1dd9.js');
require('./_commonjsHelpers-10109b76.js');
require('./index-fe328b96.js');

const sv = {
  Add: "Lägg till",
};

function create(language) {
  return index.dist.create({
    sv,
  }, language);
}

const styleCss = ".sc-ig-addresses-h{display:block}smoothly-trigger.sc-ig-addresses{display:inline-block;margin:5px}smoothly-icon.sc-ig-addresses{cursor:pointer;float:right;margin-top:-1.5em;padding-bottom:.5em;visibility:hidden}ig-address.sc-ig-addresses{clear:both}.sc-ig-addresses-h:hover smoothly-icon.sc-ig-addresses{visibility:visible}";

const IntergiroAddresses = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.changed = index$1.createEvent(this, "changed", 7);
    this.allowed = [];
    this.editing = undefined;
    this.adding = false;
  }
  get allowedArray() {
    if (!this.allowedArrayValue)
      this.allowedArrayValue = Array.isArray(this.allowed)
        ? this.allowed
        : this.allowed.split(" ");
    return this.allowedArrayValue;
  }
  get address() {
    if (typeof this.value == "string")
      this.value = JSON.parse(this.value);
    return this.value;
  }
  get addresses() {
    return index$2.Address.is(this.address) ? { primary: this.address } : Object.assign({}, this.address);
  }
  get types() {
    return index$2.Addresses.types.filter(type => index$2.Addresses.is(this.value) ? !this.value[type] : index$2.Address.is(this.value) ? type != "primary" : type);
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  onChange(e) {
    if (index$2.Address.is(e.detail)) {
      const type = e.detail.type;
      const address = Object.assign({}, e.detail);
      delete address.type;
      let result;
      if (type == "primary" && index$2.Address.is(this.address))
        result = address;
      else {
        result = Object.assign({}, this.addresses);
        result[type] = address;
      }
      this.changed.emit((this.value = result));
    }
  }
  onCreate(e) {
    const type = e.detail.type;
    const value = e.detail.value;
    const result = this.addresses;
    result[type] = value;
    this.changed.emit((this.value = result));
    this.adding = false;
  }
  onTrigger(e) {
    if (e.detail.name == "add" && typeof this.value != "string") {
      const result = this.addresses;
      const added = index$2.Address.create("SE");
      const type = e.detail.value;
      result[type] = added;
      this.value = result;
    }
  }
  onDelete(e) {
    const type = e.detail;
    const result = Object.assign({}, this.addresses);
    delete result[type];
    this.changed.emit((this.value = result));
  }
  render() {
    const allowed = this.allowedArray.filter(type => index$2.Addresses.is(this.address) ? this.address[type] == undefined : type != "primary");
    return [
      this.adding ? (index$1.h("ig-address-input", { onClose: () => (this.adding = false), mode: "add", types: this.types, onCreate: (e) => this.onCreate(e) })) : ([]),
      this.types.length > 0 ? (index$1.h("smoothly-icon", { name: "add-circle", toolTip: this.t("Add"), fill: "clear", size: "medium", color: "success", style: this.adding ? { display: "none" } : {}, onClick: () => (this.adding = true) })) : ([]),
      this.address ? (index$2.Address.is(this.address) ? (index$1.h("ig-address", { mode: this.editing ? "edit" : "display", value: this.address, onEdit: (e) => this.onChange(e), onDelete: (e) => this.onDelete(e) })) : (index$2.Addresses.map(this.address, (type, address) => (index$1.h("ig-address", { mode: this.editing ? "edit" : "display", type: type, value: address, onEdit: (e) => this.onChange(e), onDelete: (e) => this.onDelete(e) }))))) : ([]),
      allowed.map(type => (index$1.h("smoothly-trigger", { color: "primary", fill: "clear", name: "add", value: type }, "+", type))),
    ];
  }
  get element() { return index$1.getElement(this); }
};
IntergiroAddresses.style = styleCss;

exports.ig_addresses = IntergiroAddresses;
