import { Component, Event, Fragment, h, Prop, State } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '../../../../functions/fetch';
export class ScSubscriptionCancel {
  async cancelSubscription() {
    var _a, _b;
    try {
      this.error = '';
      this.busy = true;
      await apiFetch({
        path: addQueryArgs(`/surecart/v1/subscriptions/${(_a = this.subscription) === null || _a === void 0 ? void 0 : _a.id}/cancel`, {
          cancellation_act: {
            ...(!!this.comment ? { comment: this.comment } : {}),
            cancellation_reason_id: (_b = this.reason) === null || _b === void 0 ? void 0 : _b.id,
          },
        }),
        method: 'PATCH',
      });
      this.scCancelled.emit();
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
      this.busy = false;
    }
  }
  renderContent() {
    var _a, _b;
    if (this.loading) {
      return this.renderLoading();
    }
    return (h(Fragment, null, ((_a = this === null || this === void 0 ? void 0 : this.protocol) === null || _a === void 0 ? void 0 : _a.cancel_behavior) === 'pending' ? (h("div", { slot: "description" },
      __('Your plan will be canceled, but is still available until the end of your billing period on', 'surecart'),
      ' ',
      h("strong", null,
        h("sc-format-date", { type: "timestamp", date: (_b = this === null || this === void 0 ? void 0 : this.subscription) === null || _b === void 0 ? void 0 : _b.current_period_end_at, month: "long", day: "numeric", year: "numeric" })),
      ". ",
      __('If you change your mind, you can renew your subscription.', 'surecart'))) : (h("div", { slot: "description" }, __('Your plan will be canceled immediately. You cannot change your mind.', 'surecart')))));
  }
  renderLoading() {
    return (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%' } })));
  }
  render() {
    return (h("sc-dashboard-module", { heading: this.heading || __('Cancel your plan', 'surecart'), class: "subscription-cancel", error: this.error, style: { '--sc-dashboard-module-spacing': '1em' } },
      this.renderContent(),
      h("sc-flex", { justifyContent: "flex-start" },
        h("sc-button", { type: "primary", loading: this.loading || this.busy, disabled: this.loading || this.busy, onClick: () => this.cancelSubscription() }, __('Cancel Plan', 'surecart')),
        h("sc-button", { style: { color: 'var(--sc-color-gray-500' }, type: "text", onClick: () => this.scAbandon.emit(), loading: this.loading || this.busy, disabled: this.loading || this.busy }, __('Keep My Plan', 'surecart'))),
      this.busy && h("sc-block-ui", null)));
  }
  static get is() { return "sc-subscription-cancel"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-subscription-cancel.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-subscription-cancel.css"]
  }; }
  static get properties() { return {
    "heading": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "heading",
      "reflect": false
    },
    "backUrl": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "back-url",
      "reflect": false
    },
    "successUrl": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "success-url",
      "reflect": false
    },
    "subscription": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Subscription",
        "resolved": "Subscription",
        "references": {
          "Subscription": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "protocol": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "SubscriptionProtocol",
        "resolved": "SubscriptionProtocol",
        "references": {
          "SubscriptionProtocol": {
            "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
    }
  }; }
  static get states() { return {
    "loading": {},
    "busy": {},
    "error": {}
  }; }
  static get events() { return [{
      "method": "scAbandon",
      "name": "scAbandon",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "void",
        "resolved": "void",
        "references": {}
      }
    }, {
      "method": "scCancelled",
      "name": "scCancelled",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "void",
        "resolved": "void",
        "references": {}
      }
    }]; }
}
