import{$ as a}from"./iframe-DSGyX7_4.js";class h{constructor(t){this.input=t,this.api=t.data("api"),this.tileWrapper=t.closest("form").next(".tiles"),this.allResults=a('<a href="#" class="btn has-icon icon-small-arrow-right-simple" />').text("Alle Suchergebnisse"),this.buttonWrapper=a('<span class="all-button-wrapper" />').append(this.allResults),this.resultCount=a('<span class="count" />').appendTo(this.allResults),this.handleKeyEvents()}handleKeyEvents(){this.input.on("input",()=>{this.input.val().length>0&&this.loadData(this.input.val().toLowerCase())})}loadData(t){fetch(this.api.replace("{SEARCHTERM}",t)).then(e=>e.json()).then(e=>{this.buildResults(e)})}buildResults(t){let e=6;t.results.count>0&&this.tileWrapper.html(""),t.downloads.count>0&&(this.buildGroup(t.downloads,"downloads","Downloads"),e=e-1),t.events.count>0&&(this.buildGroup(t.events,"events","Veranstaltungen"),e=e-1),t["a-z"].count>0&&(this.buildGroup(t["a-z"],"a-z","IHK von A bis Z"),e=e-1),t.contacts.count>0&&(this.buildGroup(t.contacts,"contacts","Ansprechpartner"),e=e-1),t.results.items.map((s,i)=>{i<e&&this.buildResult(s)}),t.proposals.count>0&&this.buildProposals(t.proposals),this.tileWrapper.append(this.buttonWrapper),this.resultCount.text(t.results.count)}buildResult(t){const e=a("<div/>").addClass("tile").appendTo(this.tileWrapper),s=a("<a/>").attr("href",t.url).appendTo(e),i=a("<span/>").addClass("inner").appendTo(s);a("<span/>").addClass("item-title").appendTo(i).text(this.cropTitle(t.title,t.kicker.length>0)),t.kicker.length>0&&a("<span/>").addClass("kicker").text(t.kicker).prependTo(i),t.type.length>0&&e.attr("data-type",t.type)}cropTitle(t,e){const s=e?60:80;return t.length>s?t.substr(0,s)+"…":t}buildGroup(t,e,s){const i=a("<div/>").addClass("tile").addClass(e).appendTo(this.tileWrapper),p=a("<div/>").addClass("outer").addClass("variant-subtle-primary").appendTo(i),n=a("<div/>").addClass("inner").appendTo(p),r=a("<p/>").appendTo(n),o=a("<ul/>").appendTo(n);a("<a/>").attr("href",t.url).text(s+" ("+t.count+")").appendTo(r),t.items.map((l,d)=>{if(d<2){const u=a("<li/>").appendTo(o);a("<a/>").attr("title",l.title).attr("href",l.url).text(l.title.length>48?l.title.substr(0,48)+"…":l.title).appendTo(u)}})}buildProposals(t){this.tileWrapper.find(".proposals").remove();const e=a("<div/>").addClass("proposals").appendTo(this.tileWrapper),s=a("<p/>").text("Meinten Sie vielleicht: ").appendTo(e);t.items.map(i=>{const p=a("<a/>").attr("href",i.url).text(i.title);s.append(p)})}}a("body").on("ihk-init dynamic-component-loaded gfi-dynamic-init",function(){a(".search-field.typeahead:not(.initiated)").each(function(){new h(a(this))})});export{h as I};
