'use strict';

const index$1 = require('./index-0b1e2673.js');
const index$4 = require('./index-1a9e1965.js');
require('./index-451ca284.js');
const index$2 = require('./index-2eb96242.js');
const index = require('./index-36cf3884.js');
const index$5 = require('./index-061d0ccd.js');
const index$3 = require('./index-10a0b195.js');
const index$6 = require('./index-a802b56a.js');
const index$7 = require('./index-8a61808f.js');

const sv = {
  Deselect: "Avmarkera",
  Select: "Markera",
  Link: "Länk",
  "Send receipt as email to customer": "Sänd kvitto via email till kunden",
  "Open receipt as pdf": "Öppna kvitto i pdf-format",
  "Sending receipt...": "Skickar kvitto...",
  "Successfully sent email": "E-post skickat",
  "Failed to send email": "Misslyckades med att skicka e-post",
  unknown: "okänd",
};

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

const styleCss = ".sc-payfunc-orders-table-row-h{display:table-row}.sc-payfunc-orders-table-row-h>td.sc-payfunc-orders-table-row>*.sc-payfunc-orders-table-row{margin-bottom:-1px;margin-top:-1px}.sc-payfunc-orders-table-row-h>td.sc-payfunc-orders-table-row>.openable.sc-payfunc-orders-table-row{padding:.6em .6em;width:max-content}.sc-payfunc-orders-table-row-h>td.sc-payfunc-orders-table-row>.openable.sc-payfunc-orders-table-row:hover{background-color:rgb(var(--smoothly-dark-shade));cursor:pointer;border-color:rgb(var(--smoothly-medium-shade))}.sc-payfunc-orders-table-row-h>td.sc-payfunc-orders-table-row>.openable.opened.sc-payfunc-orders-table-row{border:solid 1px;border-color:rgb(var(--smoothly-medium-shade));background-color:rgb(var(--smoothly-dark-shade));border-bottom-color:rgba(80,80,80,1)}.sc-payfunc-orders-table-row-h>td.sc-payfunc-orders-table-row>smoothly-checkbox.sc-payfunc-orders-table-row{display:inline-block;vertical-align:-40%;margin-left:.3em}smoothly-icon.sc-payfunc-orders-table-row{display:inline-block;margin-left:.4em}smoothly-icon.receipt.sc-payfunc-orders-table-row{vertical-align:-24%;cursor:pointer}.sc-payfunc-orders-table-row-h>td.sc-payfunc-orders-table-row:last-child>div.sc-payfunc-orders-table-row{width:100%;max-width:20em;display:flex;align-content:stretch;flex-wrap:nowrap}.sc-payfunc-orders-table-row-h>td.sc-payfunc-orders-table-row>div.sc-payfunc-orders-table-row>payfunc-status.sc-payfunc-orders-table-row{margin-right:.2em;padding-right:0.5em;flex-shrink:5;width:100%}";

const PayfuncOrdersTableRow = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.orderSelected = index$1.createEvent(this, "orderSelected", 7);
    this.columnClicked = index$1.createEvent(this, "columnClicked", 7);
    this.notice = index$1.createEvent(this, "notice", 7);
    this.display = false;
  }
  async componentWillLoad() {
    this.t = create(this.element);
    await this.getAudience();
  }
  onChecked(event) {
    this.orderSelected.emit({ order: this.order, checked: !this.selected });
    event.stopPropagation();
    event.preventDefault();
  }
  async getAudience() {
    var _a, _b, _c;
    if (index$2.Connection.key) {
      const unpacked = await ((_a = index$3.dist.Verifier.create()) === null || _a === void 0 ? void 0 : _a.verify(index$2.Connection.key));
      this.audience = unpacked
        ? typeof unpacked.aud == "string"
          ? unpacked.aud
          : (_c = (_b = unpacked.aud) === null || _b === void 0 ? void 0 : _b.reduce((p, c) => (p == "account" ? p : c), "")) !== null && _c !== void 0 ? _c : ""
        : "";
    }
    else
      this.audience = "";
  }
  handleColumnClick(column) {
    this.columnClicked.emit({ id: this.order.id, column });
  }
  async sendReceiptEmail() {
    this.notice.emit(index$4.Notice.execute(this.t("Sending receipt..."), async () => {
      const response = await index$2.Connection.get(`order/${this.order.id}/receipt?email=true`);
      let result;
      if (!index$5.dist.Error.is(response))
        result = [true, this.t("Successfully sent email")];
      else
        result = [false, this.t("Failed to send email")];
      return result;
    }));
  }
  render() {
    var _a, _b, _c, _d, _e, _f;
    const t = this.t;
    return [
      !this.display ? (index$1.h("td", null, index$1.h("smoothly-checkbox", { selected: this.selected }))) : (""),
      index$1.h("td", null, index$1.h("div", { class: "openable" + (this.opened == "number" ? " opened" : ""), onClick: () => this.handleColumnClick("number") }, this.order.number)),
      !this.display ? (index$1.h("td", null, index$1.h("div", { class: "openable" + (this.opened == "customer" ? " opened" : ""), onClick: () => this.handleColumnClick("customer") }, (_a = index$2.Customer.getLabel(this.order.customer)) !== null && _a !== void 0 ? _a : (index$2.Payment.Defer.is(this.order.payment) ? this.order.payment.contact : t("unknown"))))) : (""),
      index$1.h("td", null, index$1.h("smoothly-display-amount", { class: "openable" + (this.opened == "items" ? " opened" : ""), onClick: () => this.handleColumnClick("items"), amount: index$2.Item.amount(this.order.items), currency: this.order.currency })),
      this.receipt ? (index$1.h("td", null, this.order.payment.type == "card" && [
        index$1.h("a", { href: index$2.Connection.baseUrl +
            "order/" +
            this.order.id +
            "/receipt?merchant=" +
            index$2.Connection.key +
            "&accept=application%2Fpdf", target: "_blank" }, index$1.h("smoothly-icon", { class: "receipt", name: "receipt", size: "small", toolTip: t("Open receipt as pdf") })),
        this.audience != "account" && ((_b = this.order.customer) === null || _b === void 0 ? void 0 : _b.email) && (index$1.h("smoothly-icon", { class: "receipt", name: "mail", size: "small", toolTip: t("Send receipt as email to customer"), onClick: () => this.sendReceiptEmail() })),
      ])) : (""),
      index$1.h("td", null, index$1.h("div", { class: "openable" + (this.opened == "payment" ? " opened" : ""), onClick: () => this.handleColumnClick("payment") }, index$2.Payment.Card.is(this.order.payment)
        ? index$6.dist.format(this.order.payment.iin + "******" + this.order.payment.last4, "card-number")
        : index$2.Payment.Defer.is(this.order.payment) && ((_c = this.order.customer) === null || _c === void 0 ? void 0 : _c.email)
          ? this.order.payment.contact
          : index$2.Payment.Defer.is(this.order.payment) && this.order.payment.link
            ? this.order.payment.link.substring(0, 8) +
              "..." +
              this.order.payment.link.slice(this.order.payment.link.length - 16)
            : "")),
      index$1.h("td", null, index$1.h("smoothly-display-date-time", { class: "openable" + (this.opened == "created" ? " opened" : ""), onClick: () => this.handleColumnClick("created"), datetime: this.order.created ? index$7.dist.DateTime.localize(index$7.dist.DateTime.parse(this.order.created)) : "" })),
      index$1.h("td", null, index$1.h("div", null, this.order.status
        ? Array.isArray(this.order.status)
          ? (_e = (_d = this.order.status) === null || _d === void 0 ? void 0 : _d.map((status) => index$1.h("payfunc-status", { status: status }))) !== null && _e !== void 0 ? _e : [] : (_f = Object.entries(this.order.status).map(status => index$2.Status.is(status[0]) && index$1.h("payfunc-status", { status: status[0] }))) !== null && _f !== void 0 ? _f : []
        : [])),
    ];
  }
  get element() { return index$1.getElement(this); }
};
PayfuncOrdersTableRow.style = styleCss;

exports.PayfuncOrdersTableRow = PayfuncOrdersTableRow;
