'use strict';

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

const index$1 = require('./index-df541290.js');
const index$3 = require('./index-b8688617.js');
require('./index-5e992066.js');
const index$2 = require('./index-c12b8888.js');
const index = require('./index-b7493032.js');
const index$4 = require('./index-3e5f1089.js');
require('./_commonjsHelpers-10109b76.js');
require('./App-1e388484.js');
require('./index-0b6f1dd9.js');
require('./index-fe328b96.js');

const sv = {
  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(language) {
  return index.dist.create({
    sv,
  }, language);
}

const styleCss = ".sc-ig-address-input-h{display:block;margin-top:.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:.2em}smoothly-select#types.sc-ig-address-input{display:grid;width:100% !important;border:none;padding-bottom:.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:.3em;padding-right:.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 = index$2.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(this.element);
    if (!this.value) {
      this.value = index$2.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 = index$2.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$3.dist.CountryCode.Name.parse(e.detail.country.toLowerCase())
        : this.value.countryCode });
    if (result.countryCode == "DE")
      result.state = this.state;
    if ((index$2.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(index$4.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), index$2.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$3.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;

exports.ig_address_input = IntergiroAddressInput;
