import { r as registerInstance, j as createEvent, h, g as getElement } from './index-aeac5cad.js';
import { N as Notice } from './index-4920b0b3.js';
import './index-66cd4113.js';
import { C as Connection, a as Customer, P as Payment, I as Item, S as Status } from './index-df45deab.js';
import { d as dist } from './index-fdd60c34.js';
import { d as dist$2 } from './index-909faba2.js';
import { d as dist$1 } from './index-6b5931f4.js';
import { d as dist$3 } from './index-ff2f9e0e.js';
import { d as dist$4 } from './index-31b724c4.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 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>.openable.sc-payfunc-orders-table-row{padding:.5em .3em;width:max-content;min-width:3em;display:inline-block}.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}.sc-payfunc-orders-table-row-h>td.sc-payfunc-orders-table-row>.openable.opened.sc-payfunc-orders-table-row{border:solid 1px rgb(var(--smoothly-medium-shade));background-color:rgb(var(--smoothly-dark-shade));border-bottom:3px solid rgb(var(--smoothly-dark-shade));border-radius:0 0 2px 2px;margin-bottom:-2px}.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) {
    registerInstance(this, hostRef);
    this.orderSelected = createEvent(this, "orderSelected", 7);
    this.columnClicked = createEvent(this, "columnClicked", 7);
    this.notice = 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 (Connection.key) {
      const unpacked = await ((_a = dist$1.Verifier.create()) === null || _a === void 0 ? void 0 : _a.verify(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(Notice.execute(this.t("Sending receipt..."), async () => {
      const response = await Connection.get(`order/${this.order.id}/receipt?email=true`);
      let result;
      if (!dist$2.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 ? (h("td", null, h("smoothly-checkbox", { selected: this.selected }))) : (""),
      h("td", null, h("div", { class: "openable" + (this.opened == "number" ? " opened" : ""), onClick: () => this.handleColumnClick("number") }, this.order.number)),
      !this.display ? (h("td", null, h("div", { class: "openable" + (this.opened == "customer" ? " opened" : ""), onClick: () => this.handleColumnClick("customer") }, (_a = Customer.getLabel(this.order.customer)) !== null && _a !== void 0 ? _a : (Payment.Defer.is(this.order.payment) ? this.order.payment.contact : t("unknown"))))) : (""),
      h("td", null, h("smoothly-display-amount", { class: "openable" + (this.opened == "items" ? " opened" : ""), onClick: () => this.handleColumnClick("items"), amount: Item.amount(this.order.items), currency: this.order.currency })),
      this.receipt ? (h("td", null, this.order.payment.type == "card" && [
        h("a", { href: Connection.baseUrl +
            "order/" +
            this.order.id +
            "/receipt?merchant=" +
            Connection.key +
            "&accept=application%2Fpdf", target: "_blank" }, 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) && (h("smoothly-icon", { class: "receipt", name: "mail", size: "small", toolTip: t("Send receipt as email to customer"), onClick: () => this.sendReceiptEmail() })),
      ])) : (""),
      h("td", null, h("div", { class: "openable" + (this.opened == "payment" ? " opened" : ""), onClick: () => this.handleColumnClick("payment") }, Payment.Card.is(this.order.payment)
        ? dist$3.format(this.order.payment.iin + "******" + this.order.payment.last4, "card-number")
        : Payment.Defer.is(this.order.payment) && ((_c = this.order.customer) === null || _c === void 0 ? void 0 : _c.email)
          ? this.order.payment.contact
          : 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)
            : "")),
      h("td", null, h("smoothly-display-date-time", { class: "openable" + (this.opened == "created" ? " opened" : ""), onClick: () => this.handleColumnClick("created"), datetime: this.order.created ? dist$4.DateTime.localize(dist$4.DateTime.parse(this.order.created)) : "" })),
      h("td", null, 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) => h("payfunc-status", { status: status }))) !== null && _e !== void 0 ? _e : [] : (_f = Object.entries(this.order.status).map(status => Status.is(status[0]) && h("payfunc-status", { status: status[0] }))) !== null && _f !== void 0 ? _f : []
        : [])),
    ];
  }
  get element() { return getElement(this); }
};
PayfuncOrdersTableRow.style = styleCss;

export { PayfuncOrdersTableRow as P };
