import{h}from"@stencil/core";import{__}from"@wordpress/i18n";import apiFetch from"../../../../functions/fetch";export class ScCancelSurvey{constructor(){this.protocol=void 0,this.reasons=void 0,this.loading=void 0,this.selectedReason=void 0,this.comment=void 0,this.error=void 0}componentWillLoad(){this.reasons||this.fetchReasons()}handleSelectedReasonChange(){var e;(null===(e=this.selectedReason)||void 0===e?void 0:e.comment_enabled)&&setTimeout((()=>{this.textArea.triggerFocus()}),50)}async fetchReasons(){try{this.loading=!0,this.reasons=await apiFetch({path:"surecart/v1/cancellation_reasons"})}catch(e){console.error(e),this.error=e}finally{this.loading=!1}}async handleSubmit(e){e.preventDefault(),e.stopImmediatePropagation();try{this.scSubmitReason.emit({reason:this.selectedReason,comment:this.comment})}catch(e){console.error(e),this.error=e}}renderReasons(){return this.loading?h("sc-choice",null,h("sc-skeleton",null)):(this.reasons||[]).map((e=>h("sc-choice",{value:null==e?void 0:e.id,name:"reason",onScChange:t=>{t.target.checked&&(this.selectedReason=e)}},null==e?void 0:e.label)))}render(){var e,t,s;const{reasons_title:o,reasons_description:n,skip_link:a}=(null===(e=this.protocol)||void 0===e?void 0:e.preservation_locales)||{};return this.loading?h("sc-skeleton",null):h("div",{class:"cancel-survey"},h("sc-dashboard-module",{heading:o,style:{"--sc-dashboard-module-spacing":"2em"}},h("span",{slot:"description"},n),h("sc-form",{onScSubmit:e=>this.handleSubmit(e),style:{"--sc-form-row-spacing":"2em"}},h("sc-choices",{showLabel:!1,label:__("Choose a reason","surecart"),style:{"--columns":"2"},required:!0},this.renderReasons()),(null===(t=this.selectedReason)||void 0===t?void 0:t.comment_enabled)&&h("sc-textarea",{label:(null===(s=this.selectedReason)||void 0===s?void 0:s.comment_prompt)||__("Additional Comments","surecart"),required:!0,ref:e=>this.textArea=e,onScInput:e=>this.comment=e.target.value}),h("sc-flex",{justifyContent:"flex-start"},h("sc-button",{type:"primary",submit:!0},__("Continue","surecart"),h("sc-icon",{name:"arrow-right",slot:"suffix"})),!!a&&h("sc-button",{class:"cancel-survey__abort-link",type:"text",onClick:()=>this.scAbandon.emit()},a)))))}static get is(){return"sc-cancel-survey"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-cancel-survey.scss"]}}static get styleUrls(){return{$:["sc-cancel-survey.css"]}}static get properties(){return{protocol:{type:"unknown",mutable:!1,complexType:{original:"SubscriptionProtocol",resolved:"SubscriptionProtocol",references:{SubscriptionProtocol:{location:"import",path:"../../../../types",id:"src/types.ts::SubscriptionProtocol"}}},required:!1,optional:!1,docs:{tags:[],text:""}},reasons:{type:"unknown",mutable:!0,complexType:{original:"CancellationReason[]",resolved:"CancellationReason[]",references:{CancellationReason:{location:"import",path:"../../../../types",id:"src/types.ts::CancellationReason"}}},required:!1,optional:!1,docs:{tags:[],text:""}}}}static get states(){return{loading:{},selectedReason:{},comment:{},error:{}}}static get events(){return[{method:"scAbandon",name:"scAbandon",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:""},complexType:{original:"void",resolved:"void",references:{}}},{method:"scSubmitReason",name:"scSubmitReason",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:""},complexType:{original:"{ reason: CancellationReason; comment: string }",resolved:"{ reason: CancellationReason; comment: string; }",references:{CancellationReason:{location:"import",path:"../../../../types",id:"src/types.ts::CancellationReason"}}}}]}static get watchers(){return[{propName:"selectedReason",methodName:"handleSelectedReasonChange"}]}}