let self,store;class OptionsGetter{constructor(t){store=t,self=this}async getter(t){let e=self.getRequiredParam(t.params);self.updateOptionsStatus(e,"awaiting");try{let s=self.getEndpointURL(t.type,t.params),o=await fetch(s,{method:"get",headers:{"Content-Type":"application/json","X-WP-Nonce":window.tablesome_ajax_object.rest_nonce}}),a=await o.json();if("success"==a.status&&a.data.length>0){self.updateOptionsStatus(e,"success");let s=a.data;t.params.post_id&&(s=self.attachIntegrationAndPostIdPropertyToOption(s,t.params)),store.workflow.update((e=>(e.options[t.type]=[...e.options[t.type],...s],e)))}}catch(t){self.updateOptionsStatus(e,"fail")}}getEndpointURL(t,e){let s=self.getEndpoint(t),o=window.tablesome_ajax_object.api_endpoints[s],a=new URL(o);for(let[t,s]of Object.entries(e))"post_id"==t&&(t="id"),a.searchParams.append(t,s);return a}getEndpoint(t){return{posts:"workflow_posts",fields:"workflow_fields",terms:"workflow_terms",taxonomies:"workflow_taxonomies",post_types:"workflow_post_types",user_roles:"workflow_user_roles",users:"workflow_users"}[t]}attachIntegrationAndPostIdPropertyToOption(t,e){return t.map((t=>(t.integration_and_post_id=self.getRequiredParam(e),t)))}getRequiredParam(t){let e=t.integration_type;return t.post_id?e=t.integration_type+"_"+t.post_id:t.post_type?e=t.post_type:t.get_post_types?e=t.get_post_types:t.get_user_roles?e=t.get_user_roles:t.get_users&&(e=t.get_users),e}updateOptionsStatus(t,e){store.workflow.update((s=>(s.optionsStatus[t]=e,s))),store.workflow.update((t=>t))}}export default OptionsGetter;