import{g as p}from"./api-DZ3cXm1m.js";function _(){var f;const o=(f=window.pageConfig)==null?void 0:f.linkable;if(!o)return;const a=$("#linkable_key"),e=$("#linkable_id"),i=$("#linkable_reference");if(!a.length||!e.length)return;const c=window.translations||{},h=c.link_target_ph||"Search…";function s(t){if(e.data("select2")&&e.select2("destroy"),!t){e.empty().prop("disabled",!0).val("").trigger("change.select2"),i.text("");return}e.prop("disabled",!1).select2({theme:"bootstrap-5",placeholder:h,allowClear:!0,width:"100%",minimumInputLength:0,ajax:{url:o.searchUrl,dataType:"json",delay:300,headers:{Authorization:`Bearer ${p()}`},data:n=>({type:t,q:n.term||""}),processResults:n=>({results:n.results||[]})}})}async function u(t,n){if(i.text(""),!(!t||!n))try{const d=`${o.referenceUrl}?type=${encodeURIComponent(t)}&id=${encodeURIComponent(n)}`,r=await(await fetch(d,{headers:{Authorization:`Bearer ${p()}`,Accept:"application/json"}})).json();if(r&&r.reference){const m=r.reference.time==="year"?c.link_ref_year||"year":c.link_ref_month||"month",g=c.link_reference_cost||"Reference cost: :amount :currency / :time";i.text(g.replace(":amount",r.reference.amount).replace(":currency",r.reference.currency_code||"").replace(":time",m))}}catch{}}const l=a.val();if(l){s(l);const t=e.data("current-id");t&&u(l,t)}else e.prop("disabled",!0);a.on("change",function(){s(this.value)}),e.on("change",function(){u(a.val(),e.val())})}export{_ as i};
