import { Component, h, State, Event, Listen, Watch, Prop, Host } from '@stencil/core';
import { __ } from '@wordpress/i18n';
/**
 * This component listens for a confirmed event and redirects to the success url.
 */
export class ScFormErrorProvider {
  /** Trigger the error event when an error happens  */
  handleErrorUpdate(val) {
    this.scUpdateError.emit(val);
  }
  handleOrderChange() {
    this.error = null;
  }
  /** Listen for error events in component. */
  handleErrorEvent(e) {
    this.error = e.detail;
    if (Object.keys((e === null || e === void 0 ? void 0 : e.detail) || {}).length) {
      this.scSetState.emit('REJECT'); // make sure we are rejecting the current state.
    }
  }
  /** Listen for pay errors. */
  handlePayError(e) {
    var _a;
    this.error = ((_a = e.detail) === null || _a === void 0 ? void 0 : _a.message) || {
      code: '',
      message: 'Something went wrong with your payment.',
    };
  }
  /** 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 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() {
    return (h(Host, null,
      !!this.errorMessage() && (h("sc-alert", { type: "danger", scrollOnOpen: true, open: !!this.errorMessage() },
        h("span", { slot: "title" }, this.errorMessage()))),
      h("slot", null)));
  }
  static get is() { return "sc-form-error-provider"; }
  static get encapsulation() { return "shadow"; }
  static get properties() { return {
    "order": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Order",
        "resolved": "Order",
        "references": {
          "Order": {
            "location": "import",
            "path": "../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The current order."
      }
    }
  }; }
  static get states() { return {
    "error": {}
  }; }
  static get events() { return [{
      "method": "scUpdateError",
      "name": "scUpdateError",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Set the state."
      },
      "complexType": {
        "original": "ResponseError",
        "resolved": "ResponseError",
        "references": {
          "ResponseError": {
            "location": "import",
            "path": "../../../types"
          }
        }
      }
    }, {
      "method": "scSetState",
      "name": "scSetState",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Form state event."
      },
      "complexType": {
        "original": "FormStateSetter",
        "resolved": "\"EXPIRE\" | \"FETCH\" | \"FINALIZE\" | \"PAID\" | \"REJECT\" | \"RESOLVE\"",
        "references": {
          "FormStateSetter": {
            "location": "import",
            "path": "../../../types"
          }
        }
      }
    }]; }
  static get watchers() { return [{
      "propName": "error",
      "methodName": "handleErrorUpdate"
    }, {
      "propName": "order",
      "methodName": "handleOrderChange"
    }]; }
  static get listeners() { return [{
      "name": "scError",
      "method": "handleErrorEvent",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "scPayError",
      "method": "handlePayError",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
