import { r as registerInstance, j as createEvent, h, g as getElement } from './index-aeac5cad.js';
import './_commonjsHelpers-91fffb88.js';
import { c as Address, d as Addresses } from './index-ec2a4942.js';
import { d as dist } from './index-fdd60c34.js';

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

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

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

const PayfuncAddresses = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.changed = 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 Address.is(this.address) ? { primary: this.address } : Object.assign({}, this.address);
  }
  get types() {
    return Addresses.types.filter(type => Addresses.is(this.value) ? !this.value[type] : Address.is(this.value) ? type != "primary" : type);
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  onChange(e) {
    if (Address.is(e.detail)) {
      const type = e.detail.type;
      const address = Object.assign({}, e.detail);
      delete address.type;
      let result;
      if (type == "primary" && 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 = 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 => Addresses.is(this.address) ? this.address[type] == undefined : type != "primary");
    return [
      this.adding ? (h("payfunc-address-input", { onClose: () => (this.adding = false), mode: "add", types: this.types, onCreate: (e) => this.onCreate(e) })) : ([]),
      this.types.length > 0 ? (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 ? (Address.is(this.address) ? (h("payfunc-address", { mode: this.editing ? "edit" : "display", value: this.address, onEdit: (e) => this.onChange(e), onDelete: (e) => this.onDelete(e) })) : (Addresses.map(this.address, (type, address) => (h("payfunc-address", { mode: this.editing ? "edit" : "display", type: type, value: address, onEdit: (e) => this.onChange(e), onDelete: (e) => this.onDelete(e) }))))) : ([]),
      allowed.map(type => (h("smoothly-trigger", { color: "primary", fill: "clear", name: "add", value: type }, "+", type))),
    ];
  }
  get element() { return getElement(this); }
};
PayfuncAddresses.style = styleCss;

export { PayfuncAddresses as payfunc_addresses };
