import { Component, Event, h, Prop, State } from '@stencil/core';
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '../../../../functions/fetch';
import { getHumanDiscount } from '../../../../functions/price';
import { replaceAmount } from './functions';
export class ScCancelDiscount {
  replaceAmount(string) {
    var _a, _b;
    if (!((_a = this.protocol) === null || _a === void 0 ? void 0 : _a.preservation_coupon)) {
      return string;
    }
    return replaceAmount(string, getHumanDiscount((_b = this.protocol) === null || _b === void 0 ? void 0 : _b.preservation_coupon));
  }
  async addDiscount() {
    var _a, _b;
    try {
      this.loading = true;
      this.subscription = (await apiFetch({
        method: 'PATCH',
        path: addQueryArgs(`surecart/v1/subscriptions/${(_a = this.subscription) === null || _a === void 0 ? void 0 : _a.id}/preserve`, {
          cancellation_act: {
            ...(!!this.comment ? { comment: this.comment } : {}),
            cancellation_reason_id: (_b = this.reason) === null || _b === void 0 ? void 0 : _b.id,
          },
        }),
      }));
      this.scPreserved.emit();
    }
    catch (e) {
      console.error(e);
      this.error = e;
    }
    finally {
      this.loading = false;
    }
  }
  render() {
    var _a;
    const { preserve_title, preserve_description, preserve_button, cancel_link } = ((_a = this.protocol) === null || _a === void 0 ? void 0 : _a.preservation_locales) || {};
    return (h("div", { class: "cancel-discount" },
      h("sc-dashboard-module", { heading: this.replaceAmount(preserve_title), style: { '--sc-dashboard-module-spacing': '2em' } },
        h("span", { slot: "description" }, this.replaceAmount(preserve_description)),
        h("sc-flex", { justifyContent: "flex-start" },
          h("sc-button", { type: "primary", onClick: () => this.addDiscount() }, preserve_button),
          h("sc-button", { class: "cancel-discount__abort-link", type: "text", onClick: () => this.scCancel.emit() }, cancel_link)),
        !!this.loading && h("sc-block-ui", { spinner: true }))));
  }
  static get is() { return "sc-cancel-discount"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-cancel-discount.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-cancel-discount.css"]
  }; }
  static get properties() { return {
    "subscription": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Subscription",
        "resolved": "Subscription",
        "references": {
          "Subscription": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "reason": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "CancellationReason",
        "resolved": "CancellationReason",
        "references": {
          "CancellationReason": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "comment": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "comment",
      "reflect": false
    },
    "protocol": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "SubscriptionProtocol",
        "resolved": "SubscriptionProtocol",
        "references": {
          "SubscriptionProtocol": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "loading": {},
    "error": {}
  }; }
  static get events() { return [{
      "method": "scCancel",
      "name": "scCancel",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "void",
        "resolved": "void",
        "references": {}
      }
    }, {
      "method": "scPreserved",
      "name": "scPreserved",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "void",
        "resolved": "void",
        "references": {}
      }
    }]; }
}
