import{o as e,t}from"./payload-DyKkZIOX.mjs";function n(e){let t=e.createElement(`style`);return t.textContent=`
.bk-picker-toolbar {
  display: grid;
  gap: var(--dt-space-3);
  margin-bottom: var(--dt-space-4);
}

.bk-picker-search {
  width: 100%;
  box-sizing: border-box;
  padding: var(--dt-space-3) var(--dt-space-4);
  border: 1px solid var(--dt-border-default);
  border-radius: var(--dt-radius-lg);
  background: var(--dt-bg-primary);
  color: var(--dt-text-primary);
  font: inherit;
}

.bk-picker-done {
  justify-self: start;
  border: 0;
  border-radius: var(--dt-radius-lg);
  padding: var(--dt-space-3) var(--dt-space-5);
  background: var(--dt-accent-fg);
  color: var(--dt-accent-subtle);
  cursor: pointer;
  font: inherit;
  font-weight: 600;
}

.bk-card--selected {
  position: relative;
  outline: 2px solid var(--dt-accent-fg);
  outline-offset: 2px;
  box-shadow: 0 0 0 1px var(--dt-accent-fg), var(--dt-shadow-md);
}

.bk-card-checkmark {
  position: absolute;
  top: var(--dt-space-3);
  right: var(--dt-space-3);
  width: var(--dt-space-6);
  height: var(--dt-space-6);
  border-radius: 999px;
  background: var(--dt-success-fg);
  color: var(--dt-success-subtle);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: var(--dt-font-size-sm);
  font-weight: 700;
}
`,t}function r(t){return Array.isArray(t)?t.flatMap(t=>!e(t)||typeof t.id!=`string`||typeof t.label!=`string`?[]:[{id:t.id,label:t.label}]):[]}function i(t){return Array.isArray(t)?t.flatMap(t=>!e(t)||typeof t.id!=`string`||typeof t.label!=`string`?[]:[{id:t.id,label:t.label,category:typeof t.category==`string`?t.category:void 0,tags:Array.isArray(t.tags)?t.tags.filter(e=>typeof e==`string`):[]}]):[]}function a(t){if(!e(t))return[];let n=r(t.categories),a=i(t.items);if(n.length===0)return a;let o=[],s=new Set;for(let e of n)for(let t of a)t.category===e.id&&(o.push(t),s.add(t.id));for(let e of a)s.has(e.id)||o.push(e);return o}function o(e,t){e.classList.toggle(`bk-card--selected`,t);let n=e.querySelector(`.bk-card-checkmark`);if(t){if(!n){let t=e.ownerDocument.createElement(`span`);t.className=`bk-card-checkmark`,t.textContent=`✓`,e.appendChild(t)}return}n?.remove()}function s(e,r={}){let i=a(t(e.ownerDocument));if(i.length===0)return()=>{};let s=r.container??e.ownerDocument.body;if(Array.from(s.querySelectorAll(`.bk-cards`))[0]!==e)return()=>{};let c=Array.from(s.querySelectorAll(`.bk-card`)).flatMap((e,t)=>{let n=i[t];return n?[{card:e,item:n}]:[]}),l=e.ownerDocument,u=n(l),d=l.createElement(`div`);d.className=`bk-picker-toolbar`;let f=l.createElement(`input`);f.className=`bk-picker-search`,f.placeholder=`Filter items`;let p=l.createElement(`button`);p.type=`button`,p.className=`bk-picker-done`,p.textContent=`Done`,d.append(f,p),e.parentElement?.insertBefore(d,e),l.head?.appendChild(u);let m=new Set,h=()=>{let e=f.value.toLowerCase().trim();for(let{card:t,item:n}of c){let r=`${n.label} ${n.tags.join(` `)}`.toLowerCase();t.hidden=e.length>0&&!r.includes(e)}},g=[];for(let{card:e,item:t}of c){e.dataset.pickerId=t.id,e.tabIndex=0;let n=()=>{if(m.has(t.id)){m.delete(t.id),o(e,!1);return}m.add(t.id),o(e,!0)},r=()=>{n()},i=e=>{e.key!==`Enter`&&e.key!==` `||(e.preventDefault(),n())};e.addEventListener(`click`,r),e.addEventListener(`keydown`,i),g.push(()=>{e.removeEventListener(`click`,r),e.removeEventListener(`keydown`,i),e.hidden=!1,e.removeAttribute(`data-picker-id`),e.removeAttribute(`tabindex`),o(e,!1)})}let _=()=>{h()},v=()=>{r.onAction?.(`pick`,JSON.stringify(Array.from(m)))};return f.addEventListener(`input`,_),p.addEventListener(`click`,v),g.push(()=>{f.removeEventListener(`input`,_),p.removeEventListener(`click`,v)}),h(),()=>{for(let e of g)e();d.remove(),u.remove()}}export{s as hydratePicker};