'use strict';

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

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

const sv = {
  "Subscription number": "Prenumerationsnummer",
  Advanced: "Avancerat",
  "Create subscription": "Skapa prenumeration",
  Subscription: "Prenumeration",
  "Start date": "Startdatum",
  "End date": "Slutdatum",
  "Cancelling subscription...": "Avbryter prenumerationen ...",
  "Subscription was cancelled.": "Prenumerationen avbröts.",
  "Failed to cancel subscription.": "Misslyckades med att avbryta prenumerationen.",
  "Cancel subscription": "Avbryt prenumeration",
  "Unknown date": "Okänt datum",
  Ongoing: "Tillsvidare",
  Number: "Nummer",
  None: "Ingen",
  Start: "Start",
  End: "Avslut",
  Amount: "Summa",
  Schedule: "Schema",
  "Invalid Schedule": "Ogiltigt Schema",
  "Next Payment": "Nästa Betalning",
  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",
  every: "var",
  year: "år",
  month: "månad",
  quarter: "kvartal",
  week: "vecka",
  day: "dag",
  of: "av",
  "of every": "av varje",
  "to last": "sista",
  "starting from the": "med början",
  "starting from": "med början",
  "on the": "den",
  monday: "måndag",
  tuesday: "tisdag",
  wednesday: "onsdag",
  thursday: "torsdag",
  friday: "fredag",
  saturday: "lördag",
  sunday: "söndag",
  january: "januari",
  february: "februari",
  march: "mars",
  april: "april",
  june: "juni",
  july: "juli",
  august: "augusti",
  september: "september",
  october: "oktober",
  november: "november",
  december: "december",
  ordinals: (data) => {
    let result = data;
    if (data.match(/^\d+(st|nd|rd|th)$/)) {
      let numberPart = Number.parseInt(data);
      numberPart = Math.abs(Math.floor(numberPart));
      result = numberPart.toString().match(/(^(1|2)|[^1](1|2))$/) ? numberPart + "a" : numberPart + "e";
    }
    return result;
  },
  onThe: (data) => {
    let result = data;
    let matchResult;
    if ((matchResult = data.match(/on the \d+(?:st|nd|rd|th) (?:month|year|quarter|day|week)/)) != null &&
      matchResult.length > 0) {
      let indexBefore = 0;
      let indexAfter = 0;
      result = data.replace(/on the \d+(?:st|nd|rd|th) (?:month|year|quarter|day|week)/, "¤" + (indexBefore += 1));
      matchResult.forEach(r => {
        let index;
        while ((index = r.search(/\d(st|nd|rd|th)/)) > -1)
          r = r.substring(0, index + 1) + r.substring(index + 3, r.length);
        const rCadenceMatch = r.match(/(month|year|quarter|day|week)/);
        const cadenceLength = rCadenceMatch != null && rCadenceMatch.length > 0 ? rCadenceMatch[0].length : 1;
        const rDigitsMatch = r.match(/\d+/);
        const digitsLength = rDigitsMatch != null && rDigitsMatch.length > 0 ? rDigitsMatch[0].length : 1;
        const digits = r.substring("on the ".length, "on the ".length + digitsLength);
        const cadence = r.substring(r.length - cadenceLength, r.length);
        result = result.replace("¤" + (indexAfter += 1), cadence + " " + digits);
      });
    }
    return result;
  },
};

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

const styleCss = ".sc-customer-subscriptions-panel-h{max-width:63em;min-width:44em;min-height:20em;margin:auto;margin-top:2em;margin-bottom:2em;border:1px solid rgb(var(--smoothly-medium-color));font-family:var(--smoothly-font-family)}[hidden].sc-customer-subscriptions-panel-h{display:none}[component=\"subscription\"].sc-customer-subscriptions-panel-h{margin:1em 0 1em 0}.sc-customer-subscriptions-panel-h>section.sc-customer-subscriptions-panel{display:block;width:100%;-ms-flex-direction:row;flex-direction:row;-ms-flex-pack:left;justify-content:left;-ms-flex-wrap:wrap;flex-wrap:wrap;display:-ms-flexbox;display:flex;padding-top:1em;padding-bottom:1em;background-color:rgba(var(--smoothly-dark-color), var(--background-alpha))}.sc-customer-subscriptions-panel-h>h2.sc-customer-subscriptions-panel{display:-ms-flexbox;display:flex;margin-top:0em;margin-bottom:0;padding-bottom:1em;padding-left:1.5em;padding-top:1em;-ms-flex-pack:justify;justify-content:space-between}.sc-customer-subscriptions-panel-h>section.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel>row.sc-customer-subscriptions-panel{margin-bottom:1em}column.sc-customer-subscriptions-panel>row.sc-customer-subscriptions-panel{-ms-flex-pack:left;justify-content:left;-ms-flex-wrap:wrap;flex-wrap:wrap;display:-ms-flexbox;display:flex}row.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel{-ms-flex-direction:row;flex-direction:row}section.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel>row.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel>row.sc-customer-subscriptions-panel:nth-of-type(2){margin-left:5%}h2.sc-customer-subscriptions-panel>smoothly-icon.sc-customer-subscriptions-panel{margin-right:1em;cursor:pointer}ig-future-due-dates.sc-customer-subscriptions-panel,.sc-customer-subscriptions-panel-h>section.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel>row.third.sc-customer-subscriptions-panel{margin-left:0em;-ms-flex-align:baseline;align-items:baseline}ig-future-due-dates.sc-customer-subscriptions-panel{display:-ms-flexbox;display:flex;min-width:13em;margin-top:0em;margin-bottom:1em}.pointer.sc-customer-subscriptions-panel{cursor:pointer}.pointer.sc-customer-subscriptions-panel:hover{text-shadow:0 0 0px rgb(var(--smoothly-secondary-shade))}section.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel{margin-left:5%}section.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel:first-of-type>row.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel>row.sc-customer-subscriptions-panel>h1.sc-customer-subscriptions-panel{min-width:8em}section.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel:first-of-type>row.sc-customer-subscriptions-panel,section.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel:first-of-type>row.sc-customer-subscriptions-panel>column.sc-customer-subscriptions-panel>row.sc-customer-subscriptions-panel{margin-right:0.5em}column.sc-customer-subscriptions-panel{margin-right:0em;margin-top:0em;margin-bottom:0em;display:-ms-flexbox;display:flex;-ms-flex-pack:start;justify-content:start;-ms-flex-direction:column;flex-direction:column;-ms-flex-align:baseline;align-items:baseline}column.sc-customer-subscriptions-panel>row.sc-customer-subscriptions-panel{margin:0%;margin-right:2em;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column}column.sc-customer-subscriptions-panel>row.sc-customer-subscriptions-panel>h1.sc-customer-subscriptions-panel{margin:0em}h1.sc-customer-subscriptions-panel{display:-ms-flexbox;display:flex;margin:5%;margin-top:0em;margin-bottom:0em;text-transform:capitalize}column.sc-customer-subscriptions-panel>row.sc-customer-subscriptions-panel>smoothly-display.sc-customer-subscriptions-panel{margin:0%;max-width:23em}smoothly-display.sc-customer-subscriptions-panel{display:-ms-flexbox;display:flex;margin:5%;margin-top:0em;margin-bottom:0em}";

const CustomerSubscriptionsPanel = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.notice = index$1.createEvent(this, "notice", 7);
    this.detailedScheduleText = true;
  }
  componentWillLoad() {
    this.t = create(this.element);
    this.updateDueDates();
  }
  updateDueDates() {
    var _a;
    const dueDates = [];
    let date = (_a = this.subscription.due) !== null && _a !== void 0 ? _a : this.subscription.start;
    for (let i = 0; i < 5; i++) {
      date = index$2.Schedule.next(this.subscription.schedule, date);
      if (date && (!this.subscription.end || date < this.subscription.end))
        dueDates.push(date);
    }
    this.dueDates = dueDates;
  }
  cancel() {
    this.notice.emit(Notice.Notice.execute(this.t("Cancelling subscription..."), async () => {
      const response = await Handler.Handler.client.customer.cancelSubscription("me", this.subscription.id);
      let result;
      if (Array.isArray(response) && response.every(index$2.Subscription.is)) {
        const subscription = response.find(s => this.subscription.id == s.id);
        if (subscription) {
          this.subscription = subscription;
          this.updateDueDates();
          if (subscription.end && subscription.end <= index$3.dist.DateTime.now().substring(0, 10))
            result = [true, this.t("Subscription was cancelled.")];
        }
      }
      return !result ? [false, this.t("Failed to cancel subscription.")] : result;
    }));
  }
  scheduleTranslation(schedule, short) {
    const t = this.t;
    let result = index$2.Schedule.toString(schedule, short);
    result = this.translateHelper(result, "to last");
    result = this.translateHelper(result, "starting from the");
    result = this.translateHelper(result, "starting from");
    result = t("onThe", "doesTranslationExist") != "onThe" ? t("onThe", result) : result;
    result = this.translateHelper(result, "on the");
    const wordSplit = result.split(" ");
    result = wordSplit.reduce((r, c) => {
      const ordinalsTranslation = t("ordinals", c);
      return (!r ? "" : r + " ") + t(ordinalsTranslation != "ordinals" ? ordinalsTranslation : c);
    }, "");
    return result;
  }
  translateHelper(input, translateString) {
    const t = this.t;
    const translated = t(translateString);
    if (input.split(translateString).length > 0) {
      const splitted = input.split(translateString);
      input = splitted.join(translated);
    }
    return input;
  }
  getCustomCssVariables() {
    var _a, _b, _c, _d, _e, _f;
    const result = {};
    if (this.cosmetic && (typeof this.cosmetic == "object" || ((_a = this.cosmetic) === null || _a === void 0 ? void 0 : _a.length) > 0)) {
      const cosmetic = typeof this.cosmetic == "string" && this.cosmetic.length > 0 ? JSON.parse(this.cosmetic) : this.cosmetic;
      Object.entries({
        "--smoothly-font-family": (_b = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.fontFamily) !== null && _b !== void 0 ? _b : cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.font_family,
        "--smoothly-dark-contrast": (_c = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.body) === null || _c === void 0 ? void 0 : _c.color,
        "--smoothly-dark-color": (_d = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.body) === null || _d === void 0 ? void 0 : _d.background,
        "--smoothly-light-color": (_e = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.header) === null || _e === void 0 ? void 0 : _e.background,
        "--smoothly-light-contrast": (_f = cosmetic === null || cosmetic === void 0 ? void 0 : cosmetic.header) === null || _f === void 0 ? void 0 : _f.color,
      }).forEach(([key, value]) => {
        value && (result[key] = value);
      });
    }
    return result;
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const t = this.t;
    return (index$1.h(index$1.Host, { style: this.getCustomCssVariables() }, index$1.h("h2", { color: "light" }, t(index$2.Frequency.is(this.subscription.schedule) ? this.subscription.schedule : this.subscription.schedule.frequency), " ", t("Subscription"), this.subscription.end && this.subscription.end <= index$3.dist.Date.now() ? ("") : (index$1.h("smoothly-icon", { name: "trash", size: "small", fill: "clear", color: "dark", toolTip: t("Cancel subscription"), onClick: () => this.cancel() }))), index$1.h("section", { color: "dark" }, index$1.h("column", null, index$1.h("row", null, index$1.h("h1", null, t("Start"), ":"), index$1.h("smoothly-display", { type: "text", value: (_a = this.subscription.start.substring(0, 10)) !== null && _a !== void 0 ? _a : t("Unknown date") })), index$1.h("row", null, index$1.h("h1", null, t("End"), ":"), index$1.h("smoothly-display", { type: "text", value: (_c = (_b = this.subscription.end) === null || _b === void 0 ? void 0 : _b.substring(0, 10)) !== null && _c !== void 0 ? _c : t("Ongoing") })), index$1.h("row", null, index$1.h("h1", null, t("Schedule"), ":"), index$1.h("smoothly-display", { class: "pointer", type: "text", onClick: () => (this.detailedScheduleText = !this.detailedScheduleText), value: (_d = this.scheduleTranslation(this.subscription.schedule, this.detailedScheduleText)) !== null && _d !== void 0 ? _d : t("Invalid Schedule") })), index$1.h("row", null, index$1.h("column", null, index$1.h("row", null, index$1.h("h1", null, t("Next Payment"), ":"), index$1.h("smoothly-display", { type: "text", value: (_f = (_e = this.subscription.due) === null || _e === void 0 ? void 0 : _e.substring(0, 10)) !== null && _f !== void 0 ? _f : t("None") })), index$1.h("row", { class: "middle" }, index$1.h("h1", null, t("Amount"), ":"), index$1.h("smoothly-display-amount", { amount: index$2.Item.amount(this.subscription.items).toString(), currency: this.subscription.currency }))))), index$1.h("column", null, index$1.h("row", { class: "third" }, index$1.h("h1", null, t("Number"), ":"), index$1.h("smoothly-display", { type: "text", value: (_g = this.subscription.number) !== null && _g !== void 0 ? _g : this.subscription.id })), index$1.h("row", { class: "third" }, ((_h = this.dueDates) === null || _h === void 0 ? void 0 : _h.length) > 0 ? index$1.h("ig-future-due-dates", { dates: this.dueDates }) : "")))));
  }
  get element() { return index$1.getElement(this); }
};
CustomerSubscriptionsPanel.style = styleCss;

exports.customer_subscriptions_panel = CustomerSubscriptionsPanel;
