import{__toESM}from"../../_virtual/_rolldown/runtime.js";import{require_picocolors}from"../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js";import{m,q}from"../../node_modules/.pnpm/@clack_core@1.3.1/node_modules/@clack/core/dist/index.js";import{renderMultiselectPrompt,renderSearchableSelect,renderSelectPrompt}from"./prompt-ui.js";import{filterOptions,initialSelectState,orderedSelection,reduceSelect,selectValueAtCursor,submitRowIndex}from"./select-state.js";var import_picocolors=__toESM(require_picocolors(),1);function toPromptState(e){return e}var SelectComponent=class extends m{options;multiple;search;required;filter=``;optionCursor=0;selectedSet=new Set;constructor(e){super({render:e.render,validate:()=>this.submitError()},!1),this.options=e.options,this.multiple=e.multiple,this.search=e.search,this.required=e.required,this.filter=e.initial.filter,this.optionCursor=e.initial.cursor,this.selectedSet=e.initial.selected,this.refreshValue(),this.on(`key`,(e,t)=>{if(this.multiple&&t?.name===`space`){this.apply({type:`toggle`});return}if(this.search){if(t?.name===`backspace`||e===``||e===`\b`){this.apply({type:`backspace`});return}e!==void 0&&e.length===1&&e>=` `&&e!==``&&this.apply({type:`char`,char:e})}}),this.on(`cursor`,e=>{e===`up`||e===`left`?this.apply({type:`up`}):(e===`down`||e===`right`)&&this.apply({type:`down`})})}visibleOptions(){return filterOptions(this.options,this.filter)}onSubmitRow(){return this.multiple&&this.optionCursor===submitRowIndex(this.visibleOptions())}submitLabel(){if(this.required)return`Submit`;let e=new Set(this.options.filter(e=>e.locked).map(e=>e.value));return[...this.selectedSet].some(t=>!e.has(t))?`Submit`:`Skip`}_shouldSubmit(){return!this.multiple||this.onSubmitRow()?!0:(this.apply({type:`toggle`}),!1)}selectedValues(){if(this.multiple)return orderedSelection(this.options,this.selectedSet);let e=selectValueAtCursor(this.visibleOptions(),this.optionCursor);return e===void 0?[]:[e]}submitDisplay(){if(this.multiple){let e=this.options.filter(e=>this.selectedSet.has(e.value)).map(e=>e.label);return e.length>0?e.join(`, `):import_picocolors.default.dim(`(none selected)`)}let e=selectValueAtCursor(this.visibleOptions(),this.optionCursor);return e===void 0?``:this.labelForValue(e)}labelForValue(e){return this.options.find(t=>t.value===e)?.label??e}submitError(){return this.multiple?this.required&&this.selectedSet.size===0?`Select at least one option, then press enter.`:void 0:selectValueAtCursor(this.visibleOptions(),this.optionCursor)===void 0?`Type to match an option, then press enter.`:void 0}apply(e){let t=reduceSelect({filter:this.filter,cursor:this.optionCursor,selected:this.selectedSet},e,{options:this.options,submitRow:this.multiple});this.filter=t.filter,this.optionCursor=t.cursor,this.selectedSet=t.selected,this.refreshValue()}refreshValue(){this.value=this.multiple?orderedSelection(this.options,this.selectedSet):selectValueAtCursor(this.visibleOptions(),this.optionCursor)}};function renderSelectComponent(e,t,n,r){let s=toPromptState(e.state);return e.search?renderSearchableSelect({colors:import_picocolors.default,state:s,leadingRail:n,message:t.message,multiple:e.multiple,filter:e.filter,placeholder:t.placeholder,options:e.visibleOptions(),cursor:e.optionCursor,selectedValues:e.selectedValues(),submitDisplay:e.submitDisplay(),footerNote:r,error:e.error,submitLabel:e.submitLabel()}):e.multiple?renderMultiselectPrompt({colors:import_picocolors.default,cursor:e.optionCursor,error:e.error,footerNote:r,leadingRail:n,message:t.message,options:e.options,selectedValues:e.selectedValues(),state:s,submitLabel:e.submitLabel()}):renderSelectPrompt({colors:import_picocolors.default,cursor:e.optionCursor,footerNote:r,leadingRail:n,message:t.message,options:e.options,state:s})}async function runSelectComponent(e){let toKey=e=>String(e),t=new Map,n=e.options.map(e=>{let n=toKey(e.value);return t.set(n,e.value),{value:n,label:e.label,hint:e.hint,description:e.description,disabled:e.disabled,disabledReason:e.disabledReason,locked:e.locked,lockedReason:e.lockedReason,featured:e.featured}}),i=initialSelectState({options:n,defaultValue:e.defaultValue===void 0?void 0:toKey(e.defaultValue),initialValues:e.initialValues?.map(toKey),submitRow:e.multiple}),a,o,s=new SelectComponent({options:n,multiple:e.multiple,search:e.search,required:e.required,initial:i,render(){return a?renderSelectComponent(a,{message:e.message,placeholder:e.placeholder},e.leadingRail,o?.note()):``}});a=s,o=e.attachGuard?.(s);let c=await s.prompt();return q(c)?c:e.multiple?c.map(e=>t.get(e)??e):t.get(c)??c}export{SelectComponent,runSelectComponent};