import { Component, h, Event, Listen, Watch, Prop } from '@stencil/core';
import { __ } from '@wordpress/i18n';
/**
 * @part base - The elements base wrapper.
 * @part icon - The alert icon.
 * @part text - The alert text.
 * @part title - The alert title.
 * @part message - The alert message.
 * @part close - The close icon.
 */
export class ScFormErrorProvider {
  /** Trigger the error event when an error happens  */
  handleErrorUpdate(val) {
    this.scUpdateError.emit(val);
  }
  /** Listen for error events in component. */
  handleErrorEvent(e) {
    this.error = e.detail;
  }
  /** 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 !!this.errorMessage() ? (h("sc-alert", { exportparts: "base, icon, text, title, message, close", type: "danger", scrollOnOpen: true, open: !!this.errorMessage() },
      h("span", { slot: "title" }, this.errorMessage()))) : null;
  }
  static get is() { return "sc-error"; }
  static get encapsulation() { return "shadow"; }
  static get properties() { return {
    "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."
      }
    }
  }; }
  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"
          }
        }
      }
    }]; }
  static get watchers() { return [{
      "propName": "error",
      "methodName": "handleErrorUpdate"
    }]; }
  static get listeners() { return [{
      "name": "scError",
      "method": "handleErrorEvent",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
