'use strict';

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

const index$1 = require('./index-140bcbcd.js');
const index = require('./index-d3079b93.js');
const index$2 = require('./index-bddf9814.js');
require('./App-0b3e3f31.js');
require('./Token-8c5ba5d9.js');
const Notice = require('./Notice-e970b490.js');
const Item = require('./Item-00ce8c24.js');
require('./index-ac98852d.js');
require('./Credentials-8e4749ab.js');
require('./_commonjsHelpers-537d719a.js');
require('./index-09251373.js');
require('./TextDecoder-753156f3.js');

const sv$2 = {
  Edit: "Redigera",
  Delete: "Radera",
  primary: "primär",
  billing: "fakturering",
  delivery: "leverans",
  visit: "besök",
};

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

const styleCss$2 = ".sc-ig-address-h{display:block;position:relative}[hidden].sc-ig-address-h{display:none}smoothly-icon.sc-ig-address{cursor:pointer;margin-top:0.3em}label.sc-ig-address{display:inline-block;-webkit-margin-before:0.6em;margin-block-start:0.6em;-webkit-margin-after:0.2em;margin-block-end:0.2em;text-transform:capitalize;margin-bottom:0.2em}smoothly-popup.sc-ig-address{display:none;position:absolute;right:0}.sc-ig-address-h:hover smoothly-popup.sc-ig-address{display:inline}";

const IntergiroAddress = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.edit = index$1.createEvent(this, "edit", 7);
    this.delete = index$1.createEvent(this, "delete", 7);
    this.type = "primary";
    this.mode = "display";
  }
  componentWillLoad() {
    this.t = create$2(this.element);
  }
  onChange(event) {
    event.stopPropagation();
    this.value = event.detail;
    this.mode = "display";
    this.edit.emit(Object.assign({ type: this.type }, this.value));
  }
  onDelete() {
    this.delete.emit(this.type);
  }
  render() {
    const t = this.t;
    return [
      index$1.h("smoothly-popup", null, index$1.h("smoothly-icon", { name: "ellipsis-vertical-outline", size: "small" }), index$1.h("div", { color: "light", slot: "popup" }, index$1.h("smoothly-icon", { size: "small", name: "pencil-outline", toolTip: t("Edit"), onClick: () => (this.mode = "edit") }), index$1.h("smoothly-icon", { size: "small", name: "trash", toolTip: "Delete", onClick: () => this.onDelete() }))),
      this.mode == "edit"
        ? [
          index$1.h("label", null, t(this.type)),
          index$1.h("ig-address-input", { onEdit: (e) => this.onChange(e), value: this.value }),
        ]
        : [index$1.h("label", null, t(this.type)), index$1.h("ig-address-display", { value: this.value })],
    ];
  }
  get element() { return index$1.getElement(this); }
};
IntergiroAddress.style = styleCss$2;

const sv$1 = {
  Street: "Gata",
  "Zip code": "Postnummer",
  City: "Ort",
  Country: "Land",
  Save: "Spara",
  Cancel: "Avbryt",
  "Unknown address country": "Okänt adressland",
  primary: "primär",
  billing: "fakturering",
  delivery: "leverans",
  visit: "besök",
  "Please enter a valid address.": "Ange en giltig adress.",
};

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

const styleCss$1 = ".sc-ig-address-input-h{display:block;margin-top:0.5em}[hidden].sc-ig-address-input-h{display:none}form.sc-ig-address-input{display:-ms-flexbox;display:flex;-ms-flex-flow:row;flex-flow:row;-ms-flex-wrap:wrap;flex-wrap:wrap;border-collapse:collapse}smoothly-input.sc-ig-address-input{border:1px solid rgb(var(--smoothly-medium-color))}smoothly-input[name=street]#add.sc-ig-address-input,smoothly-input[name=address1]#add.sc-ig-address-input{width:calc(100% - 3.8em - 3px)}smoothly-input[name=street].sc-ig-address-input,smoothly-input[name=address1].sc-ig-address-input{width:100%}smoothly-input[name=zipCode].sc-ig-address-input,smoothly-input[name=address2].sc-ig-address-input{min-width:calc(35% - 2px);-ms-flex-positive:2;flex-grow:2;border-top:none}smoothly-input[name=city].sc-ig-address-input,smoothly-input[name=address3].sc-ig-address-input{min-width:calc(65% - 2px);border-top:none;-ms-flex-positive:1;flex-grow:1}smoothly-input[name=country].sc-ig-address-input{width:100%;border-top:none}form.sc-ig-address-input>div.sc-ig-address-input{width:100%}smoothly-submit.sc-ig-address-input,smoothly-trigger.sc-ig-address-input{width:auto;float:right;background-color:transparent;border:none;cursor:pointer;color:transparent;margin-bottom:0.2em}smoothly-select#types.sc-ig-address-input{display:grid;width:100% !important;border:none;padding-bottom:0.5em}smoothly-select#countries.sc-ig-address-input{width:3.5em;border-bottom:1px solid rgb(var(--smoothly-medium-color));border-right:none}#states.sc-ig-address-input{width:100% !important;border-top:none}smoothly-select.sc-ig-address-input{border:1px solid rgb(var(--smoothly-medium-color));padding-bottom:0.3em;padding-right:0.3em;display:grid}";

const IntergiroAddressInput = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.edit = index$1.createEvent(this, "edit", 7);
    this.create = index$1.createEvent(this, "create", 7);
    this.close = index$1.createEvent(this, "close", 7);
    this.notice = index$1.createEvent(this, "notice", 7);
    var _a, _b;
    this.types = [];
    this.mode = "edit";
    this.type = this.types[0];
    this.selected = (_b = (_a = this.address) === null || _a === void 0 ? void 0 : _a.countryCode) !== null && _b !== void 0 ? _b : "SE";
    this.countries = ["SE", "FI", "DE", "DK", "NO"];
    this.state = Item.Address.DE.is(this.address) ? this.address.state : "";
    this.adding = false;
  }
  get address() {
    return typeof this.value == "string" ? JSON.parse(this.value) : this.value;
  }
  componentWillLoad() {
    this.t = create$1(this.element);
    if (!this.value) {
      this.value = Item.Address.create("SE");
      this.adding = true;
    }
  }
  onTrigger() {
    if (this.adding)
      this.close.emit();
    else
      this.edit.emit(this.address);
  }
  onSelectionChanged(e) {
    if (e.detail.identifier == "types")
      this.type = e.detail.value;
    else if (e.detail.identifier == "countries")
      this.value = Item.Address.create(e.detail.value);
    else if (e.detail.identifier == "states")
      this.state = e.detail.value;
  }
  onSubmit(e) {
    e.stopPropagation();
    const result = Object.assign(Object.assign({}, e.detail), { countryCode: e.detail.country
        ? index$2.dist.CountryCode.Name.parse(e.detail.country.toLowerCase())
        : this.value.countryCode });
    if (result.countryCode == "DE")
      result.state = this.state;
    if ((Item.Address.is(result) && !Object.values(result).some(x => x == "")) || result.countryCode == "") {
      this.adding ? this.create.emit({ type: this.type, value: result }) : this.edit.emit(result);
    }
    else
      this.notice.emit(Notice.Notice.failed(this.t("Please enter a valid address.")));
  }
  toUpperCase(value) {
    return value.charAt(0).toUpperCase() + value.slice(1);
  }
  render() {
    var _a;
    const t = this.t;
    let inputFields = [];
    if (this.address.countryCode == "")
      inputFields = [
        index$1.h("smoothly-input", { type: "text", name: "address1", id: this.adding ? "add" : "", value: this.address.address1 }, "Address 1"),
        index$1.h("smoothly-input", { type: "text", name: "address2", value: this.address.address2 }, "Address 2"),
        index$1.h("smoothly-input", { type: "text", name: "address3", value: this.address.address3 }, "Address 3"),
      ];
    else
      inputFields = [
        index$1.h("smoothly-input", { type: "text", name: "street", id: this.adding ? "add" : "", value: this.address.street }, t("Street")),
        index$1.h("smoothly-input", { type: "postal-code", name: "zipCode", value: this.address.zipCode }, t("Zip code")),
        index$1.h("smoothly-input", { type: "text", name: "city", value: this.address.city }, t("City")),
        this.address.countryCode == "DE" ? (index$1.h("smoothly-select", { id: "states", identifier: "states", background: "transparent" }, index$1.h("option", { value: (_a = this.address.state) !== null && _a !== void 0 ? _a : "", selected: true }, this.address.state == "" || !this.address.state ? "State" : this.address.state), Item.Address.DE.State.types.map(option => (index$1.h("option", { value: option }, option))))) : ([]),
        !this.adding ? (index$1.h("smoothly-input", { type: "text", name: "country", value: this.adding ? "" : index$2.dist.CountryCode.Name.from("en", this.address.countryCode) }, t("Country"))) : ([]),
      ];
    return [
      index$1.h("form", null, this.adding
        ? [
          index$1.h("smoothly-select", { id: "types", identifier: "types", background: "transparent" }, this.types.map(option => this.type == option ? (index$1.h("option", { value: option, selected: true }, this.toUpperCase(t(option)))) : (index$1.h("option", { value: option }, this.toUpperCase(t(option)))))),
          index$1.h("smoothly-select", { id: "countries", identifier: "countries", background: "transparent" }, this.countries.map(country => (index$1.h("option", { value: country, selected: this.selected == country }, country))), index$1.h("option", { value: "" }, "Other")),
        ]
        : [], inputFields, index$1.h("div", null, index$1.h("smoothly-submit", { color: "primary", fill: "clear", prevent: true, onSubmit: (e) => this.onSubmit(e) }, index$1.h("smoothly-icon", { name: "checkmark-circle", toolTip: t("Save"), color: "success", fill: "clear", size: "small" })), index$1.h("smoothly-trigger", { color: "primary", fill: "clear" }, index$1.h("smoothly-icon", { name: "close-circle", toolTip: t("Cancel"), color: "light", fill: "clear", size: "small" })))),
    ];
  }
  get element() { return index$1.getElement(this); }
};
IntergiroAddressInput.style = styleCss$1;

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

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

const styleCss = ".sc-ig-addresses-h{display:block}.sc-ig-addresses-h label.sc-ig-addresses{font-weight:bold}.sc-ig-addresses-h:not([label]) label.sc-ig-addresses{display:none}smoothly-trigger.sc-ig-addresses{display:inline-block;margin:5px}smoothly-icon.sc-ig-addresses{cursor:pointer;float:right;margin-top:-0.2em;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 Item.Address.is(this.address) ? { primary: this.address } : Object.assign({}, this.address);
  }
  get types() {
    return Item.Addresses.types.filter(type => Item.Addresses.is(this.value) ? !this.value[type] : Item.Address.is(this.value) ? type != "primary" : type);
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  onChange(e) {
    if (Item.Address.is(e.detail)) {
      const type = e.detail.type;
      const address = Object.assign({}, e.detail);
      delete address.type;
      let result;
      if (type == "primary" && Item.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 = Item.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 => Item.Addresses.is(this.address) ? this.address[type] == undefined : type != "primary");
    return [
      index$1.h("label", null, this.label),
      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 ? (Item.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) })) : (Item.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_address = IntergiroAddress;
exports.ig_address_input = IntergiroAddressInput;
exports.ig_addresses = IntergiroAddresses;
