import { Component, h, Host, Prop } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import { openWormhole } from 'stencil-wormhole';
/**
 * This component listens for a confirmed event and redirects to the success url.
 */
export class ScCheckoutFormErrors {
  /** This filters the error message with some more client friendly error messages. */
  getErrorMessage(error) {
    if (error.code === 'order.line_items.price.blank') {
      return __('This product is no longer purchasable.', 'surecart');
    }
    return h("span", { innerHTML: error === null || error === void 0 ? void 0 : error.message });
  }
  /** First will display validation error, then main error if no validation errors. */
  errorMessage() {
    var _a, _b, _c, _d, _e, _f;
    if ((_c = (_b = (_a = this.error) === null || _a === void 0 ? void 0 : _a.additional_errors) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.message) {
      return this.getErrorMessage((_e = (_d = this.error) === null || _d === void 0 ? void 0 : _d.additional_errors) === null || _e === void 0 ? void 0 : _e[0]);
    }
    else if ((_f = this === null || this === void 0 ? void 0 : this.error) === null || _f === void 0 ? void 0 : _f.message) {
      return this.getErrorMessage(this === null || this === void 0 ? void 0 : this.error);
    }
    return '';
  }
  render() {
    // don't show component if no error message or is finalizing or updating.
    if (!this.errorMessage() || ['finalizing', 'updating'].includes(this.checkoutState)) {
      return h(Host, { style: { display: 'none' } });
    }
    return (h(Host, null,
      h("sc-alert", { type: "danger", scrollOnOpen: true, open: !!this.errorMessage() },
        h("span", { slot: "title" }, this.errorMessage())),
      h("slot", null)));
  }
  static get is() { return "sc-checkout-form-errors"; }
  static get encapsulation() { return "shadow"; }
  static get properties() { return {
    "checkoutState": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "FormState",
        "resolved": "\"confirmed\" | \"confirming\" | \"draft\" | \"expired\" | \"failure\" | \"finalizing\" | \"idle\" | \"loading\" | \"paid\" | \"paying\" | \"redirecting\" | \"updating\"",
        "references": {
          "FormState": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The current order."
      },
      "attribute": "checkout-state",
      "reflect": false
    },
    "error": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "ResponseError | null",
        "resolved": "ResponseError",
        "references": {
          "ResponseError": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Error to display."
      }
    }
  }; }
}
openWormhole(ScCheckoutFormErrors, ['checkoutState', 'error'], false);
