import { type APISelectMenuOption } from "discord-api-types/v10" import { ComponentType, createComponent } from "../component" import { Hooks } from "../../core/hooks" import { value, type CordoFunct, type CordoFunctRun } from "../../functions" import { FunctCompiler } from "../../functions/compiler" import { MaxLengthConstants } from "../../lib/constants" type SelectMenuOption = Omit & ({ value?: Value onClick?: CordoFunct | CordoFunctRun }) export function selectString() { let placeholderVal: string | undefined = undefined let labelVal: string | undefined = undefined let descriptionVal: string | undefined = undefined let minValues: number | undefined = undefined let maxValues: number | undefined = undefined let optionsVal: SelectMenuOption[] = [] let disabledVal: boolean | undefined = undefined let ref: string | undefined = undefined const functVal: CordoFunct[] = [] function getPlaceholder() { if (!placeholderVal) return undefined return Hooks.callHook( 'transformUserFacingText', placeholderVal, { component: 'StringSelect', position: 'placeholder' } ) } function getLabel() { if (!labelVal) return 'Your Response' return Hooks.callHook( 'transformUserFacingText', labelVal, { component: 'StringSelect', position: 'label' } ) } function getDescription() { if (!descriptionVal) return undefined return Hooks.callHook( 'transformUserFacingText', descriptionVal, { component: 'StringSelect', position: 'description' } ) } function getOptions(): SelectMenuOption[] { return optionsVal.slice(0, 25).map(o => ({ ...o, label: Hooks.callHook('transformUserFacingText', o.label, { component: 'StringSelect', position: 'option.label' })?.slice(0, MaxLengthConstants.SELECT_OPTION_LABEL), description: o.description ? Hooks.callHook('transformUserFacingText', o.description, { component: 'StringSelect', position: 'option.description' })?.slice(0, MaxLengthConstants.SELECT_OPTION_DESCRIPTION) : undefined, value: FunctCompiler.toCustomId([ ...(o.onClick ? Array.isArray(o.onClick) ? o.onClick : [ o.onClick ] : [] ), o.value ? value(o.value) : null ]) })) } const out = { ...createComponent('StringSelect', () => ({ type: ComponentType.StringSelect, placeholder: getPlaceholder(), min_values: minValues, max_values: maxValues ? Math.min(optionsVal.length, maxValues) : undefined, disabled: disabledVal, options: getOptions(), custom_id: FunctCompiler.toCustomId([ ...(disabledVal ? [] : functVal), ...(ref ? [ value(ref) ] : []) ]), 'modal:label': getLabel(), 'modal:description': getDescription(), })), as: (id: string) => { ref = id return out }, placeholder: (text: string) => { placeholderVal = text return out }, min: (num: number = 1) => { minValues = num return out }, max: (num: number = 25) => { maxValues = num return out }, disabled(disabled = true) { disabledVal = disabled return out }, onSubmit: (...funct: CordoFunctRun) => { functVal.push(...funct) return out }, setOptions(options: Array>) { optionsVal = options return out }, addOption(o: SelectMenuOption) { optionsVal.push(o) return out }, withLabel: (text: string) => { labelVal = text return out }, withDescription: (text: string) => { descriptionVal = text return out }, } return out }