import { r as registerInstance, j as createEvent, h, g as getElement } from './index-07845439.js';
import { E as Error } from './index-38089f62.js';
import { d as dist$2 } from './index-046ba252.js';
import { d as dist$1 } from './index-837ba18a.js';
import { V as Verifier } from './index-265fba1f.js';
import './index-cd3eb450.js';
import { C as Connection, b as Contact, P as Payment, I as Item, S as Status } from './index-e20749b7.js';
import { d as dist } from './index-63517df5.js';
import { N as Notice } from './index-f068b00b.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-ig-orders-table-row-h{display:table-row;border-top:1px solid rgba(var(--smoothly-medium-color), 0);border-bottom:1px solid rgba(var(--smoothly-medium-color), 0)}.sc-ig-orders-table-row-h:hover{-webkit-box-shadow:var(--smoothly-shadow);box-shadow:var(--smoothly-shadow);border-top-color:rgb(var(--smoothly-medium-color));border-bottom-color:rgb(var(--smoothly-medium-color))}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row>.openable.sc-ig-orders-table-row{padding:.5em .3em;width:-webkit-max-content;width:-moz-max-content;width:max-content;min-width:3em;display:inline-block}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row>.openable.sc-ig-orders-table-row:hover{background-color:rgb(var(--smoothly-dark-shade));cursor:pointer}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row>.openable.opened.sc-ig-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-ig-orders-table-row-h>td.sc-ig-orders-table-row>smoothly-checkbox.sc-ig-orders-table-row{display:inline-block;vertical-align:-40%;margin-left:.3em}smoothly-icon.sc-ig-orders-table-row{display:inline-block;margin-left:.4em}smoothly-icon.receipt.sc-ig-orders-table-row{vertical-align:-24%;cursor:pointer}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row:last-child>div.sc-ig-orders-table-row{width:100%;max-width:20em;display:-ms-flexbox;display:flex;-ms-flex-line-pack:stretch;align-content:stretch;-ms-flex-wrap:nowrap;flex-wrap:nowrap}.sc-ig-orders-table-row-h>td.sc-ig-orders-table-row>div.sc-ig-orders-table-row>ig-status.sc-ig-orders-table-row{margin-right:.2em;padding-right:0.5em;-ms-flex-negative:5;flex-shrink:5;width:100%}";

const IntergiroOrdersTableRow = 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 = 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 == "customer" ? 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`);
      return !Error.is(response) ? [true, this.t("Successfully sent email")] : [false, this.t("Failed to send email")];
    }));
  }
  render() {
    var _a, _b, _c, _d;
    const t = this.t;
    const contact = Contact.is(this.order.customer) ? this.order.customer : undefined;
    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 == "contact" ? " opened" : ""), onClick: () => this.handleColumnClick("contact") }, (_a = Contact.getLabel(contact)) !== 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 != "customer" && (contact === null || contact === void 0 ? void 0 : contact.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$1.format(this.order.payment.iin + "******" + this.order.payment.last4, "card-number")
        : Payment.Defer.is(this.order.payment) && (contact === null || contact === void 0 ? void 0 : contact.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$2.DateTime.localize(dist$2.DateTime.parse(this.order.created)) : "" })),
      h("td", null, h("div", null, this.order.status
        ? Array.isArray(this.order.status)
          ? (_c = (_b = this.order.status) === null || _b === void 0 ? void 0 : _b.map((status) => h("ig-status", { status: status }))) !== null && _c !== void 0 ? _c : []
          : (_d = Object.entries(this.order.status).map(status => Status.is(status[0]) && h("ig-status", { status: status[0] }))) !== null && _d !== void 0 ? _d : []
        : [])),
    ];
  }
  get element() { return getElement(this); }
};
IntergiroOrdersTableRow.style = styleCss;

export { IntergiroOrdersTableRow as I };
