'use strict';

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

const index$1 = require('./index-140bcbcd.js');
const index$2 = require('./index-bddf9814.js');
const Item = require('./Item-00ce8c24.js');
const index$3 = require('./index-76391849.js');
require('./Credentials-8e4749ab.js');
const Token = require('./Token-8c5ba5d9.js');
const index = require('./index-d3079b93.js');
require('./index-188cc15d.js');
const unknown = require('./unknown-38d3748c.js');
require('./App-0b3e3f31.js');
const Trigger = require('./Trigger-a1c9ba71.js');
require('./index-46adead5.js');
require('./index-0d2e9372.js');
require('./index-edc1219d.js');
const model = require('./model-7ac3bb05.js');
const PaymentDetails = require('./PaymentDetails-ea590e93.js');
require('./_commonjsHelpers-537d719a.js');
require('./index-09251373.js');
require('./TextDecoder-753156f3.js');
require('./index-3b10efe6.js');

const sv$3 = {
  "Unknown address country": "Okänt land i adress",
};

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

const styleCss$3 = ".sc-ig-display-address-h{display:block}[hidden].sc-ig-display-address-h{display:none}address.sc-ig-display-address{-webkit-margin-before:0;margin-block-start:0;-webkit-margin-after:0;margin-block-end:0;padding:0.4em 0.4em;color:rgb(var(--smoothly-medium-color));font-style:normal}";

const IntergiroDisplayAddress = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
  }
  async componentWillLoad() {
    this.t = create$3(this.element);
  }
  render() {
    const t = this.t;
    return Item.Address.SE.is(this.data) || Item.Address.FI.is(this.data) ? ([
      index$1.h("address", null, this.data.street, index$1.h("br", null), this.data.zipCode, " ", this.data.city, index$1.h("br", null), index$2.dist.CountryCode.Name.from("en", this.data.countryCode), index$1.h("br", null)),
    ]) : Item.Address.is(this.data) ? (index$1.h("p", null, t("Unknown address country"), ": ", JSON.stringify(this.data))) : Item.Addresses.is(this.data) ? (Item.Addresses.map(this.data, (type, address) => index$1.h("ig-display-address", { type: type, data: address }))) : ("");
  }
  get element() { return index$1.getElement(this); }
};
IntergiroDisplayAddress.style = styleCss$3;

const sv$2 = {
  details: "detaljer",
  "excluding VAT": "exklusive moms",
};

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

const styleCss$2 = ".sc-ig-items-display-h{display:block}[hidden].sc-ig-items-display-h{display:none}.sc-ig-items-display-h>*.sc-ig-items-display{text-align:center;margin-bottom:1em}.sc-ig-items-display-h>section.sc-ig-items-display>smoothly-display.sc-ig-items-display{font-size:140%}.sc-ig-items-display-h>details.sc-ig-items-display>summary.sc-ig-items-display{list-style-image:none}";

const IntergiroItemsDisplay = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
  }
  componentWillLoad() {
    this.t = create$2(this.element);
  }
  render() {
    const t = this.t;
    const vat = index$3.Item.vat(this.items);
    return [
      index$1.h("details", null, index$1.h("summary", null, t("details")), index$1.h("section", null, index$1.h("ig-items-list", { items: this.items, currency: this.currency }))),
      index$1.h("section", null, index$1.h("smoothly-display", { type: "price", value: index$3.Item.amount(this.items), currency: this.currency }), vat != undefined ? (index$1.h("p", { style: { fontSize: "90%", margin: "0" } }, "(", index$1.h("smoothly-display", { style: { display: "inline" }, type: "price", value: index$3.Item.amount(this.items) - vat, currency: this.currency }), "\u00A0", t("excluding VAT"), ")")) : ([])),
    ];
  }
  get element() { return index$1.getElement(this); }
};
IntergiroItemsDisplay.style = styleCss$2;

const sv$1 = {
  Invoice: "Faktura",
  "Installment Plan": "Avbetalning",
  Card: "Kort",
};

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

const styleCss$1 = ".sc-ig-payment-option-h{display:block}[hidden].sc-ig-payment-option-h{display:none}details.sc-ig-payment-option{padding:0}details.sc-ig-payment-option>summary.sc-ig-payment-option>span.sc-ig-payment-option{float:right;margin:0.2em;font-size:60%}details.sc-ig-payment-option>summary.sc-ig-payment-option{font-size:120%;list-style-type:none;cursor:pointer;padding:0.4em;color:rgba(var(--smoothly-default-contrast), 0.8);fill:rgba(var(--smoothly-default-contrast), 0.8)}[selected].sc-ig-payment-option-h>details.sc-ig-payment-option>summary.sc-ig-payment-option{color:rgb(var(--smoothly-primary-contrast));background:rgb(var(--smoothly-tertiary-color))}details.sc-ig-payment-option>summary.sc-ig-payment-option::-webkit-details-marker{display:none}details.sc-ig-payment-option>summary.sc-ig-payment-option>svg.sc-ig-payment-option{width:1em;height:auto;margin-bottom:-0.2em;margin-right:0.4em}[selected].sc-ig-payment-option-h>details.sc-ig-payment-option>summary.sc-ig-payment-option>svg.sc-ig-payment-option{fill:rgb(var(--smoothly-primary-contrast))}";

const IntergiroPaymentOption = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.paymentOptionSelected = index$1.createEvent(this, "paymentOptionSelected", 7);
  }
  componentWillLoad() {
    this.t = create$1(this.element);
  }
  componentDidLoad() {
    const summary = this.element.getElementsByTagName("summary");
    if (summary.length > 0) {
      const handle = (e) => {
        if (!this.selected)
          this.paymentOptionSelected.emit({ name: this.type, selected: this.selected });
        e.preventDefault();
      };
      summary[0].addEventListener("click", handle);
      summary[0].addEventListener("touch", handle);
    }
  }
  render() {
    const t = this.t;
    return (index$1.h("details", { open: this.selected }, index$1.h("summary", null, this.selected ? (index$1.h("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 512 512" }, index$1.h("path", { d: "M256 48C141.6 48 48 141.6 48 256s93.6 208 208 208 208-93.6 208-208S370.4 48 256 48zm-42.7 318.9L106.7 260.3l29.9-29.9 76.8 76.8 162.1-162.1 29.9 29.9-192.1 191.9z" }))) : (index$1.h("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 512 512" }, index$1.h("path", { d: "M256 48C141.601 48 48 141.601 48 256s93.601 208 208 208 208-93.601 208-208S370.399 48 256 48zm0 374.399c-91.518 0-166.399-74.882-166.399-166.399S164.482 89.6 256 89.6 422.4 164.482 422.4 256 347.518 422.399 256 422.399z" }))), t(this.name), " ", typeof this.brand == "string" ? (index$1.h("span", { class: this.brand.toLowerCase() }, this.brand)) : Array.isArray(this.brand) ? (this.brand.map(b => index$1.h("span", { class: b.toLowerCase() }, b))) : ([])), index$1.h("slot", null)));
  }
  get element() { return index$1.getElement(this); }
};
IntergiroPaymentOption.style = styleCss$1;

const sv = {
  Payment: "Betalning",
  Transfer: "Överföring",
  "No payment options available.": "Inga betalningsalternativ finns tillgänliga.",
  "Error creating payment.": "Misslyckades med att skapa betalning.",
  "Error: Order must contain items and currency.": "Inkomplett order, order måste innehålla artiklar och valuta.",
  "Error creating payment for order.": "Misslyckades med att skapa betalning för order.",
  "Error creating order.": "Misslyckades med att skapa order.",
};

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

const styleCss = "ig-payment-option.sc-ig-payment-options{border:1px solid rgb(var(--smoothly-tertiary-color));border-bottom:none}ig-payment-option.sc-ig-payment-options:first-of-type{border-top-left-radius:4px;border-top-right-radius:4px;margin-top:0.4em}ig-payment-option.sc-ig-payment-options:last-of-type{border-bottom-left-radius:4px;border-bottom-right-radius:4px;border-bottom:1px solid rgb(var(--smoothly-tertiary-color));margin-bottom:0.6em}ig-payment-option[selected].sc-ig-payment-options{border-bottom:2px solid rgb(var(--smoothly-tertiary-color));border-left:2px solid rgb(var(--smoothly-tertiary-color));border-right:2px solid rgb(var(--smoothly-tertiary-color))}ig-payment-option[selected].sc-ig-payment-options+ig-payment-option.sc-ig-payment-options{border-top:none}";

const IntergiroPaymentOptions = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.paymentFailed = index$1.createEvent(this, "paymentFailed", 7);
    this.trigger = index$1.createEvent(this, "trigger", 7);
  }
  async componentWillLoad() {
    this.t = create(this.element);
  }
  handleToggle(event) {
    console.log(event);
    this.selected = event.detail.name;
  }
  findDeferOrderId(partialOrder) {
    return "payment" in partialOrder && partialOrder.payment.type == "defer" ? partialOrder.id : undefined;
  }
  async submit(partialOrder, apiKey, merchant) {
    if (this.selected && this.details) {
      const detail = this.details.find(d => d.type == this.selected);
      if (detail && detail.element && detail.element.getPayment) {
        let payment = await detail.element.getPayment(partialOrder, apiKey);
        if (!payment) {
          this.paymentFailed.emit({ property: "noPayment", description: "noMessage" });
          console.log("3D authorization aborted.");
        }
        else {
          const submitOrder = Object.assign(Object.assign({}, partialOrder), { payment });
          let order = await this.submitHelper(this.findDeferOrderId(partialOrder), submitOrder);
          ({ order, payment } = await this.confirmPayment(order, partialOrder, payment, apiKey, merchant, detail));
          return await this.resolveOrder(order);
        }
      }
    }
  }
  async confirmPayment(order, partialOrder, payment, apiKey, merchant, detail) {
    var _a, _b;
    if (model.isVerificationError(order) && unknown.Error.is(order)) {
      if (payment && ((_a = detail.element) === null || _a === void 0 ? void 0 : _a.confirmPayment)) {
        payment = await ((_b = detail.element) === null || _b === void 0 ? void 0 : _b.confirmPayment(partialOrder, payment, apiKey, order));
        if (payment) {
          const submitOrder = Object.assign(Object.assign({}, partialOrder), { payment });
          order = await this.submitHelper(this.findDeferOrderId(partialOrder), submitOrder);
          if (model.isVerificationError(order))
            ({ order, payment } = await this.confirmPayment(order, partialOrder, payment, apiKey, merchant, detail));
        }
        else {
          this.paymentFailed.emit({ property: "noPayment", description: "noMessage" });
          console.log("3D authorization aborted.");
        }
      }
      else {
        this.paymentFailed.emit({ property: "noPayment", description: "noMessage" });
        console.log("3D authorization aborted.");
      }
    }
    return { order, payment };
  }
  resolveOrder(order) {
    return new Promise(resolve => {
      if (unknown.Error.is(order)) {
        this.paymentFailed.emit(order);
        console.log("Error creating payment.");
      }
      else if (Token.Token.is(order))
        resolve(order);
      else if (order && model.paymentHasVerify(order.payment) && order.payment.verify.url) {
        this.url = order.payment.verify.url;
        this.resolve = resolve;
      }
      else {
        this.paymentFailed.emit({ property: "noPayment", description: this.t("Error creating payment.") });
        console.log("Error creating payment.");
      }
    });
  }
  async submitHelper(id, order) {
    let result;
    const t = this.t;
    if (!order.items || !order.currency) {
      this.paymentFailed.emit({
        property: "noPayment",
        description: t("Error: Order must contain items and currency."),
      });
      console.log("Error: must have items, currency");
    }
    else
      result = id ? await this.client.order.setPayment(order.payment, id) : await this.client.order.create(order, true);
    return result;
  }
  TriggerListener(event) {
    if (Trigger.Trigger.is(event.detail) && (event.detail.name == "order" || event.detail.name == "fail") && this.resolve) {
      this.url = undefined;
      this.resolve(event.detail.value);
      event.preventDefault();
      event.stopPropagation();
    }
    else if (typeof event.detail == "object" && event.detail.name == "close" && this.verify) {
      console.log("payment-options close", event.detail);
      const order = this.verify[0];
      if (model.paymentIs(event.detail.value))
        order.payment = event.detail.value;
      this.verify[1](order);
      this.verify = undefined;
    }
    else if (Trigger.Trigger.is(event.detail) && event.detail.name == "cardTokenFailed")
      this.paymentFailed.emit(event.detail.value);
  }
  render() {
    var _a;
    const t = this.t;
    if (this.options) {
      this.details = this.options
        .map(option => PaymentDetails.paymentDetailsCreate(option, this.cosmetic))
        .filter((detail) => !!detail);
      if (!this.selected && this.details && this.details.length > 0)
        this.selected = this.details[0].type;
    }
    return [
      this.url ? (index$1.h("smoothly-dialog", { color: "primary", closable: true }, index$1.h("smoothly-frame", { url: this.url, name: "parent", style: { height: "60em" } }))) : (""),
      index$1.h("h3", null, index$3.dist.get((_a = this.flags) !== null && _a !== void 0 ? _a : {}, "transfer") ? t("Transfer") : t("Payment")),
      this.details && this.details.length > 0 ? (this.details.map(detail => (index$1.h("ig-payment-option", { id: detail.type, type: detail.type, selected: this.selected == detail.type, name: detail.name, brand: detail.brand }, detail.component)))) : (index$1.h("p", null, t("No payment options available."))),
    ];
  }
  get element() { return index$1.getElement(this); }
};
IntergiroPaymentOptions.style = styleCss;

exports.ig_display_address = IntergiroDisplayAddress;
exports.ig_items_display = IntergiroItemsDisplay;
exports.ig_payment_option = IntergiroPaymentOption;
exports.ig_payment_options = IntergiroPaymentOptions;
