import { Component, Element, h, Prop, State } from '@stencil/core';
import { sprintf, __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '../../../../functions/fetch';
import { onFirstVisible } from '../../../../functions/lazy';
export class ScChargesList {
  constructor() {
    /** Query to fetch charges */
    this.query = {
      page: 1,
      per_page: 10,
    };
    this.showPagination = true;
    this.charges = [];
    this.pagination = {
      total: 0,
      total_pages: 0,
    };
  }
  /** Only fetch if visible */
  componentWillLoad() {
    onFirstVisible(this.el, () => {
      this.getItems();
    });
  }
  /** Get items */
  async getItems() {
    try {
      this.loading = true;
      const response = (await apiFetch({
        path: addQueryArgs(`surecart/v1/charges/`, {
          expand: ['checkout', 'checkout.order'],
          ...this.query,
        }),
        parse: false,
      }));
      this.pagination = {
        total: parseInt(response.headers.get('X-WP-Total')),
        total_pages: parseInt(response.headers.get('X-WP-TotalPages')),
      };
      this.charges = (await response.json());
    }
    catch (e) {
      if (e === null || e === void 0 ? void 0 : e.message) {
        this.error = e.message;
      }
      else {
        this.error = __('Something went wrong', 'surecart');
      }
      console.error(this.error);
    }
    finally {
      this.loading = false;
      this.loaded = true;
    }
  }
  renderRefundStatus(charge) {
    if (charge === null || charge === void 0 ? void 0 : charge.fully_refunded) {
      return h("sc-tag", { type: "danger" }, __('Refunded', 'surecart'));
    }
    if (charge === null || charge === void 0 ? void 0 : charge.refunded_amount) {
      return h("sc-tag", { type: "warning" }, __('Partially Refunded', 'surecart'));
    }
    return h("sc-tag", { type: "success" }, __('Paid', 'surecart'));
  }
  renderEmpty() {
    return (h("sc-stacked-list-row", { "mobile-size": 0 },
      h("slot", { name: "empty" }, __('You have no saved payment methods.', 'surecart'))));
  }
  renderLoading() {
    return (h("sc-stacked-list-row", { style: { '--columns': '2' }, "mobile-size": 0 },
      h("div", { style: { padding: '0.5em' } },
        h("sc-skeleton", { style: { width: '30%', marginBottom: '0.75em' } }),
        h("sc-skeleton", { style: { width: '20%', marginBottom: '0.75em' } }),
        h("sc-skeleton", { style: { width: '40%' } }))));
  }
  renderContent() {
    var _a;
    if (this.loading && !this.loaded) {
      return this.renderLoading();
    }
    if (((_a = this.charges) === null || _a === void 0 ? void 0 : _a.length) === 0) {
      return this.renderEmpty();
    }
    return this.charges.map(charge => {
      var _a;
      const { currency, amount, created_at } = charge;
      return (h("sc-stacked-list-row", { style: { '--columns': '4' }, "mobile-size": 600, href: addQueryArgs(window.location.href, {
          action: 'show',
          model: 'order',
          id: (_a = charge.checkout.order) === null || _a === void 0 ? void 0 : _a.id,
        }) },
        h("strong", null,
          h("sc-format-date", { date: created_at, type: "timestamp", month: "short", day: "numeric", year: "numeric" })),
        h("sc-text", { style: { '--color': 'var(--sc-color-gray-500)' } }, sprintf(__('#%s', 'surecart'), charge.checkout.order.number)),
        h("div", null, this.renderRefundStatus(charge)),
        h("strong", null,
          h("sc-format-number", { type: "currency", value: amount, currency: currency }))));
    });
  }
  nextPage() {
    this.query.page = this.query.page + 1;
    this.getItems();
  }
  prevPage() {
    this.query.page = this.query.page - 1;
    this.getItems();
  }
  render() {
    var _a;
    return (h("sc-dashboard-module", { class: "charges-list", error: this.error },
      h("span", { slot: "heading" },
        h("slot", { name: "heading" }, this.heading || __('Payment History', 'surecart'))),
      !!this.allLink && (h("sc-button", { type: "link", href: this.allLink, slot: "end" },
        __('View all', 'surecart'),
        h("sc-icon", { name: "chevron-right", slot: "suffix" }))),
      h("sc-card", { "no-padding": true, style: { '--overflow': 'hidden' } },
        h("sc-stacked-list", null, this.renderContent())),
      this.showPagination && (h("sc-pagination", { page: this.query.page, perPage: this.query.per_page, total: this.pagination.total, totalPages: this.pagination.total_pages, totalShowing: (_a = this === null || this === void 0 ? void 0 : this.charges) === null || _a === void 0 ? void 0 : _a.length, onScNextPage: () => this.nextPage(), onScPrevPage: () => this.prevPage() })),
      this.loading && this.loaded && h("sc-block-ui", { spinner: true })));
  }
  static get is() { return "sc-charges-list"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-charges-list.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-charges-list.css"]
  }; }
  static get properties() { return {
    "query": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "{\n    page: number;\n    per_page: number;\n  }",
        "resolved": "{ page: number; per_page: number; }",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Query to fetch charges"
      },
      "defaultValue": "{\n    page: 1,\n    per_page: 10,\n  }"
    },
    "heading": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "heading",
      "reflect": false
    },
    "showPagination": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "show-pagination",
      "reflect": false,
      "defaultValue": "true"
    },
    "allLink": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "all-link",
      "reflect": false
    }
  }; }
  static get states() { return {
    "charges": {},
    "loading": {},
    "loaded": {},
    "error": {},
    "pagination": {}
  }; }
  static get elementRef() { return "el"; }
}
