'use strict';

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

const index$1 = require('./index-8bb493f3.js');
require('./App-8b8a216b.js');
require('./_commonjsHelpers-fc53004f.js');
require('./index-4a8a2c75.js');
const Notice = require('./Notice-e970b490.js');
const index$2 = require('./index-58685270.js');
const index = require('./index-b1b1dbe1.js');
const index$3 = require('./index-6c0c325b.js');
const index$5 = require('./index-e309a98b.js');
const index$6 = require('./index-aa9aab3c.js');
const index$4 = require('./index-e92f37a7.js');

const sv = {
  Qty: "Antal",
  Unit: "Enhet",
  Name: "Namn",
  Price: "Pris",
  VAT: "Moms",
  "Clear/remove item": "Töm/ta bort artikel",
};

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

const styleCss = ".sc-payfunc-item-input-h{display:block}[hidden].sc-payfunc-item-input-h{display:none}*.sc-payfunc-item-input{z-index:0}.sc-payfunc-item-input-h>form.sc-payfunc-item-input{display:flex;flex-wrap:nowrap}.sc-payfunc-item-input-h>form.sc-payfunc-item-input>smoothly-input.sc-payfunc-item-input{border:1px solid rgb(var(--smoothly-medium-color));margin-left:-1px;flex-grow:1}.sc-payfunc-item-input-h>form.sc-payfunc-item-input>smoothly-input.sc-payfunc-item-input>#itemName.sc-payfunc-item-input{flex-grow:2}.sc-payfunc-item-input-h>form.sc-payfunc-item-input>smoothly-trigger.sc-payfunc-item-input{float:right;padding-top:.3em}";

const PayfuncItemInput = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.itemChanged = index$1.createEvent(this, "itemChanged", 7);
    this.itemCleared = index$1.createEvent(this, "itemCleared", 7);
    this.item = {};
  }
  componentWillLoad() {
    this.t = create(this.element);
  }
  clearInputs(e) {
    if (e.detail.name == "clear") {
      e.stopPropagation();
      e.preventDefault();
      const quantity = document.getElementById("itemQuantity");
      const unit = document.getElementById("itemUnit");
      const name = document.getElementById("itemName");
      const price = document.getElementById("itemPrice");
      const vat = document.getElementById("itemVat");
      const inputs = [quantity, unit, name, price, vat];
      for (const input of inputs)
        if (hasStringValue(input) || hasNumberValue(input))
          input.value = "";
      this.updateItem();
      this.itemCleared.emit();
    }
  }
  updateItem() {
    const quantity = document.getElementById("itemQuantity");
    const unit = document.getElementById("itemUnit");
    const name = document.getElementById("itemName");
    const price = document.getElementById("itemPrice");
    const vat = document.getElementById("itemVat");
    const item = index$2.Item.fromVatInclusivePrice(hasNumberValue(price) ? price.value : 0, hasNumberValue(vat) ? vat.value : undefined, undefined, hasStringValue(name) ? name.value : undefined, hasStringValue(quantity) && Number(quantity.value) > 0 ? Number(quantity.value) : undefined, hasStringValue(unit) ? unit.value : undefined, undefined);
    this.itemChanged.emit(item);
  }
  render() {
    const t = this.t;
    return [
      index$1.h("form", { name: "item" }, index$1.h("smoothly-input", { class: "left small", onKeyDown: this.updateItem.bind(this), id: "itemQuantity", value: this.item.quantity, type: "text", name: "quantity" }, t("Qty")), index$1.h("smoothly-input", { class: "small", onKeyDown: this.updateItem.bind(this), id: "itemUnit", value: this.item.unit, type: "text", name: "unit" }, t("Unit")), index$1.h("smoothly-input", { class: "big", onKeyDown: this.updateItem.bind(this), id: "itemName", value: this.item.name, type: "text", name: "name" }, t("Name")), index$1.h("smoothly-input", { class: "medium", onKeyDown: this.updateItem.bind(this), id: "itemPrice", value: this.inclusivePrice, type: "price", name: "price" }, t("Price")), index$1.h("smoothly-input", { class: "medium", onKeyDown: this.updateItem.bind(this), id: "itemVat", value: this.vatPercentage, type: "percent", name: "vat" }, t("VAT")), index$1.h("smoothly-trigger", { fill: "clear", name: "clear" }, index$1.h("smoothly-icon", { toolTip: t("Clear/remove item"), size: "small", name: "trash" }))),
    ];
  }
  get element() { return index$1.getElement(this); }
};
function hasStringValue(element) {
  return typeof element.value === "string";
}
function hasNumberValue(element) {
  return typeof element.value === "number";
}
PayfuncItemInput.style = styleCss;

const sv$1 = {};

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

const styleCss$1 = ".sc-payfunc-item-list-h{display:block}[hidden].sc-payfunc-item-list-h{display:none}";

const PayfuncItemList = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
  }
  componentWillLoad() {
    this.t = create$1(this.element);
  }
  render() {
    return [
      index$1.h("section", null, this.items
        ? this.items.map((item, index) => (index$1.h("payfunc-display-item", { item: item, currency: this.currency, index: index })))
        : ""),
    ];
  }
  get element() { return index$1.getElement(this); }
};
PayfuncItemList.style = styleCss$1;

const sv$2 = {
  Date: "Datum",
  Month: "Månad",
  Weekday: "Dag",
  Monday: "Måndag",
  Tuesday: "Tisdag",
  Wednesday: "Onsdag",
  Thursday: "Torsdag",
  Friday: "Fredag",
  Saturday: "Lördag",
  Sunday: "Söndag",
  January: "Januari",
  February: "Februari",
  March: "Mars",
  May: "Maj",
  June: "Juni",
  July: "Juli",
  August: "Augusti",
  October: "Oktober",
  First: "Första",
  Second: "Andra",
  Third: "Tredje",
};

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

const styleCss$2 = ".sc-payfunc-select-offset-h{display:flex;justify-content:space-between}[frequency=daily].sc-payfunc-select-offset-h{display:none}smoothly-select.sc-payfunc-select-offset{display:grid;flex-grow:2}smoothly-select.sc-payfunc-select-offset:nth-of-type(2){border-left:1px solid rgb(var(--smoothly-medium-color))}";

const PayfuncSelectOffset = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.offsetChanged = index$1.createEvent(this, "offsetChanged", 7);
    this.days = [];
  }
  componentWillLoad() {
    this.t = create$2(this.element);
    this.weekdays = ["Weekday", ...index$2.Schedule.weekdays];
    this.quarterly = ["Month", ...index$2.Schedule.quarterly];
    this.months = ["Month", ...index$2.Schedule.months];
    const days = ["Date", ...index$2.Schedule.daysOfMonth.map(day => (day >= 0 ? (day + 1).toString() : day.toString()))];
    this.days = days;
    this.setValues();
  }
  onSelectionChanged(e) {
    const value = Number(e.detail.value);
    e.detail.value = value != undefined && value >= 0 ? (value - 1).toString() : e.detail.value;
    if (e.detail.identifier == "offset") {
      this.value = e.detail.value;
      this.offsetChanged.emit(this.value2 ? [this.value, this.value2] : this.value);
    }
    else if (e.detail.identifier == "offset2") {
      this.value2 = e.detail.value;
      this.offsetChanged.emit([this.value, this.value2]);
    }
  }
  setValues() {
    switch (this.frequency) {
      case "weekly":
        this.values = this.weekdays;
        this.value = "Weekday";
        this.value2 = undefined;
        break;
      case "monthly":
        this.values = this.days;
        this.value = "Date";
        this.value2 = undefined;
        break;
      case "quarterly":
        this.values = this.quarterly;
        this.value = "Month";
        this.values2 = this.days;
        this.value2 = "Date";
        break;
      case "yearly":
        this.values = this.months;
        this.value = "Month";
        this.values2 = this.days;
        this.value2 = "Date";
        break;
    }
  }
  render() {
    return [
      this.values && this.value ? (index$1.h("smoothly-select", { identifier: "offset", background: "transparent" }, this.values.map(option => this.value == option ? (index$1.h("option", { value: option, selected: true }, this.t(option))) : (index$1.h("option", { value: option }, this.t(option)))))) : ([]),
      this.values2 && this.value2 && this.value != "Month" ? (index$1.h("smoothly-select", { identifier: "offset2", background: "transparent" }, this.values2.map(option => this.value2 == option ? (index$1.h("option", { value: option, selected: true }, this.t(option))) : (index$1.h("option", { value: option }, this.t(option)))))) : ([]),
    ];
  }
  get element() { return index$1.getElement(this); }
  static get watchers() { return {
    "frequency": ["setValues"]
  }; }
};
PayfuncSelectOffset.style = styleCss$2;

const sv$3 = {
  "Subscription number": "Prenumerationsnummer",
  Advanced: "Avancerat",
  "Create subscription": "Skapa prenumeration",
  "Start date": "Startdatum",
  "End date": "Slutdatum",
  Qty: "Antal",
  Name: "Namn",
  Price: "Pris",
  VAT: "Moms",
  daily: "Daglig",
  weekly: "Veckovis",
  monthly: "Månadsvis",
  quarterly: "Kvartalsvis",
  yearly: "Årlig",
  Day: "Dag",
  "Add item": "Lägg till artikel",
  "Creating subscription...": "Skapar prenumeration...",
  "$0 of $1 subscriptions were created.": "$0 av $1 prenumerationer skapades.",
  "Subscription was created.": "Prenumeration skapades.",
  "Failed to create $0 of $1 subscriptions.": "Kunde inte skapa $0 av $1 prenumerationer.",
  "Failed to create subscription.": "Kunde inte skapa prenumeration.",
};

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

const styleCss$3 = ".sc-payfunc-subscription-create-h{display:block;max-width:63em}[hidden].sc-payfunc-subscription-create-h{display:none}smoothly-submit.sc-payfunc-subscription-create{max-width:min-content}.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create{display:flex;flex-wrap:wrap;flex-direction:column}.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create section.sc-payfunc-subscription-create{display:flex}smoothly-trigger.sc-payfunc-subscription-create{border:none;position:absolute;right:4em;top:1.4em}.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create>main.sc-payfunc-subscription-create>content.sc-payfunc-subscription-create>*.sc-payfunc-subscription-create:not(.last):not(span),.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create>section.sc-payfunc-subscription-create>*.sc-payfunc-subscription-create:not(smoothly-trigger):not(smoothly-icon){margin-bottom:.7em;margin-left:-1px;flex-grow:1}.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create payfunc-select-offset.sc-payfunc-subscription-create,.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create payfunc-total-amount.sc-payfunc-subscription-create,.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create smoothly-select.sc-payfunc-subscription-create,.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create smoothly-input.sc-payfunc-subscription-create{border:1px solid rgb(var(--smoothly-medium-color));border-collapse:collapse}smoothly-select.sc-payfunc-subscription-create{display:grid}.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create>section.sc-payfunc-subscription-create>*.sc-payfunc-subscription-create:not(smoothly-select),.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create payfunc-total-amount.sc-payfunc-subscription-create{flex-shrink:2}.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create>section.sc-payfunc-subscription-create>*.sc-payfunc-subscription-create:first-child{flex-grow:1}.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create>main.sc-payfunc-subscription-create{display:flex;flex-direction:row}.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create>main.sc-payfunc-subscription-create>content.sc-payfunc-subscription-create{min-width:37em}.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create>main.sc-payfunc-subscription-create>content.sc-payfunc-subscription-create>section.sc-payfunc-subscription-create>smoothly-input.sc-payfunc-subscription-create:last-of-type{border-left:none}.sc-payfunc-subscription-create-h>form.sc-payfunc-subscription-create>main.sc-payfunc-subscription-create>content.sc-payfunc-subscription-create>section.sc-payfunc-subscription-create:first-of-type>*.sc-payfunc-subscription-create{display:flex;flex-wrap:nowrap;flex-grow:1}smoothly-icon.opened.sc-payfunc-subscription-create{transform:rotate(90deg);animation-duration:.5s}payfunc-future-due-dates.sc-payfunc-subscription-create{margin-left:.5em;min-width:13em}";

const PayfuncSubscriptionCreate = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.notice = index$1.createEvent(this, "notice", 7);
    this.frequency = "monthly";
    this.currency = "SEK";
    this.advanced = false;
    this.itemCount = 0;
    this.itemsVisible = false;
    this.items = [];
  }
  componentWillLoad() {
    this.t = create$3(this.element);
    this.subscriptionNumber = index$3.dist.Identifier.generate(4);
    this.calculateSchedule();
  }
  componentDidRender() {
    const divisor = document.getElementById("divisor");
    const start = document.getElementById("start");
    const end = document.getElementById("end");
    const handle = (e) => {
      this.calculateSchedule();
      e.stopPropagation();
      e.preventDefault();
    };
    if (divisor)
      divisor.addEventListener("focusout", handle);
    if (start)
      start.addEventListener("focusout", handle);
    if (end)
      end.addEventListener("focusout", handle);
  }
  submitSubscription(e) {
    const subscription = {
      number: e.detail.number,
      items: this.items.length > 0 ? this.items : index$4.dist.parse(e.detail.price, "price", this.currency),
      currency: this.currency,
      schedule: {
        frequency: this.frequency,
        divisor: e.detail.divisor ? index$2.Schedule.extractDivisor(e.detail.divisor) : undefined,
        offset: this.offset ? index$2.Schedule.extractOffset(this.offset, this.frequency) : undefined,
      },
      start: index$5.dist.Date.is(e.detail.start) ? e.detail.start : undefined,
      end: index$5.dist.Date.is(e.detail.end) ? e.detail.end : undefined,
    };
    const idArray = Object.keys(this.accounts);
    this.notice.emit(Notice.Notice.execute(this.t("Creating subscription..."), async () => {
      let result;
      const response = await Promise.all(idArray.map(id => index$2.Connection.post(`account/${id}/subscription`, subscription)));
      const [succeeded, failed] = response.reduce(([s, f], e) => (index$6.dist.Error.is(e) ? [s, ++f] : [++s, f]), [0, 0]);
      if (succeeded > 0)
        result = [
          true,
          idArray.length > 1
            ? this.t("$0 of $1 subscriptions were created.", succeeded, idArray.length)
            : this.t("Subscription was created."),
        ];
      else
        result = [
          false,
          idArray.length > 1
            ? this.t("Failed to create $0 of $1 subscriptions.", failed, idArray.length)
            : this.t("Failed to create subscription."),
        ];
      return result;
    }));
  }
  calculateSchedule() {
    let divisor, endDate, startDate;
    if (this.advanced) {
      const divisorInput = document.getElementById("divisor");
      divisor = hasValue(divisorInput) ? divisorInput.value : undefined;
    }
    if (document.getElementById("start") && document.getElementById("end")) {
      const start = document.getElementById("start");
      startDate = hasValue(start) && index$5.dist.DateTime.localize(start.value) ? start.value : index$5.dist.DateTime.now();
      const end = document.getElementById("end");
      endDate = hasValue(end) ? index$5.dist.DateTime.localize(end.value) : undefined;
    }
    this.schedule = {
      frequency: this.frequency,
      divisor: divisor ? index$2.Schedule.extractDivisor(divisor) : undefined,
      offset: this.offset ? index$2.Schedule.extractOffset(this.offset, this.frequency) : undefined,
    };
    this.dueDates = index$2.Schedule.calculateDueDates(this.schedule, 5, startDate, endDate);
  }
  onSelectionChanged(e) {
    if (e.detail.identifier == "frequency") {
      this.frequency = e.detail.value;
      this.calculateSchedule();
    }
    if (e.detail.identifier == "currency")
      this.currency = e.detail.value;
  }
  deleteItem(e) {
    const items = [];
    this.price = (Number(this.price) - index$2.Item.amount(this.items[e.detail.index])).toFixed(2);
    for (let i = 0; i < this.items.length; i++)
      if (i != e.detail.index)
        items.push(this.items[i]);
    this.items = items;
  }
  changeItem(e) {
    e.preventDefault();
    e.stopPropagation();
    this.items[this.items.length - 1] = e.detail;
    this.price = index$2.Item.amount(this.items).toFixed(2);
  }
  changeOffset(e) {
    e.preventDefault();
    e.stopPropagation();
    this.offset = e.detail;
    this.calculateSchedule();
  }
  addItem(e) {
    e.preventDefault();
    e.stopPropagation();
    this.items = [...this.items, {}];
    this.price = index$2.Item.amount(this.items).toFixed(2);
    this.itemCount++;
    this.itemsVisible = true;
  }
  render() {
    return [
      index$1.h("form", null, index$1.h("section", null, index$1.h("smoothly-input", { type: "text", name: "number", value: this.subscriptionNumber }, this.t("Subscription number")), index$1.h("payfunc-total-amount", { price: this.price, itemsvisible: this.itemCount > 1, onAddItem: e => this.addItem(e) }), index$1.h("smoothly-select", { identifier: "frequency", background: "transparent" }, index$2.Schedule.frequencies.map(option => this.frequency == option ? (index$1.h("option", { value: option, selected: true }, this.t(option))) : (index$1.h("option", { value: option }, this.t(option))))), index$1.h("smoothly-trigger", null, index$1.h("smoothly-icon", { toolTip: this.t("Advanced"), onClick: () => (this.advanced = !this.advanced), name: "arrow-forward-circle-outline", fill: "clear", size: "small", color: "medium", class: this.advanced ? "opened" : "" })), index$1.h("smoothly-submit", { color: "primary", fill: "clear", prevent: true, onSubmit: (e) => this.submitSubscription(e) }, index$1.h("smoothly-icon", { toolTip: this.t("Create subscription"), name: "add-circle", fill: "clear", size: "medium", color: "success" }))), this.advanced
        ? [
          index$1.h("section", null, index$1.h("payfunc-select-offset", { frequency: this.frequency, onOffsetChanged: e => this.changeOffset(e) }), index$1.h("smoothly-input", { type: "divisor", id: "divisor", name: "divisor", value: "0 / 1" }, this.t("Divisor"))),
        ]
        : [], index$1.h("main", null, index$1.h("content", null, index$1.h("section", null, index$1.h("smoothly-input", { type: "date", name: "start", id: "start", value: index$5.dist.DateTime.localize(index$5.dist.DateTime.parse(index$5.dist.DateTime.now())).slice(0, 10) }, this.t("Start date")), index$1.h("smoothly-input", { type: "date", name: "end", id: "end" }, this.t("End date"))), index$1.h("payfunc-item-list", { items: this.items.slice(0, this.items.length - 1), currency: this.currency }), this.itemsVisible ? index$1.h("payfunc-item-input", { key: this.itemCount, item: {} }) : []), index$1.h("payfunc-future-due-dates", { dates: this.dueDates }))),
    ];
  }
  get element() { return index$1.getElement(this); }
};
function hasValue(element) {
  return typeof element.value === "string";
}
PayfuncSubscriptionCreate.style = styleCss$3;

const sv$4 = {
  Amount: "Summa",
  "Add item": "Lägg till artikel",
};

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

const styleCss$4 = ".sc-payfunc-total-amount-h{display:flex}[hidden].sc-payfunc-total-amount-h{display:none}.sc-payfunc-total-amount-h>*.sc-payfunc-total-amount:first-child{flex-grow:1}smoothly-select.sc-payfunc-total-amount{margin-top:.25em}";

const PayfuncTotalAmount = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.addItem = index$1.createEvent(this, "addItem", 7);
    this.currencies = [
      "SEK",
      "EUR",
      "USD",
      "DKK",
      "GBP",
      "ISK",
      "NOK",
      "AUD",
      "CAD",
      "JPY",
      "NZD",
      "CHF",
      "TRY",
      "PLN",
      "RUB",
    ];
    this.currency = "SEK";
    this.itemsvisible = false;
  }
  componentWillLoad() {
    this.t = create$4(this.element);
  }
  add(e) {
    this.addItem.emit(e);
  }
  render() {
    return [
      index$1.h("smoothly-input", { disabled: this.itemsvisible, type: "price", value: this.price, name: "price" }, this.t("Amount")),
      index$1.h("smoothly-select", { identifier: "currency", background: "transparent" }, this.currencies.map(option => this.currency == option ? (index$1.h("option", { value: option, selected: true }, option)) : (index$1.h("option", { value: option }, option)))),
      index$1.h("button", { type: "button", tabindex: "-1", onClick: e => this.add(e) }, index$1.h("smoothly-icon", { toolTip: this.t("Add item"), name: "add-circle-outline", fill: "clear", size: "medium", color: "medium" })),
    ];
  }
  get element() { return index$1.getElement(this); }
};
PayfuncTotalAmount.style = styleCss$4;

exports.payfunc_item_input = PayfuncItemInput;
exports.payfunc_item_list = PayfuncItemList;
exports.payfunc_select_offset = PayfuncSelectOffset;
exports.payfunc_subscription_create = PayfuncSubscriptionCreate;
exports.payfunc_total_amount = PayfuncTotalAmount;
