"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateEnumInputs = generateEnumInputs;
const generator_1 = require("@postxl/generator");
function generateEnumInputs({ enum_ }) {
    const variants = Array.from(enum_.members.values())
        .map((variant) => `{ id: '${variant.value}', label: '${variant.description}' }`)
        .join(', ');
    const imports = new generator_1.ImportGenerator({ path: '' }).addImports({
        [(0, generator_1.toPackageName)('@postxl/utils')]: [(0, generator_1.toTypeName)('UnionOmit')],
        [(0, generator_1.toPackageName)('react')]: [(0, generator_1.toFunctionName)('useMemo')],
        [(0, generator_1.toBackendModuleLocation)('@types')]: [enum_.types.name],
        [(0, generator_1.toPackageName)('@postxl/ui-components')]: [
            (0, generator_1.toClassName)('Select'),
            (0, generator_1.toClassName)('SelectContent'),
            (0, generator_1.toClassName)('SelectItem'),
            (0, generator_1.toClassName)('SelectTrigger'),
            (0, generator_1.toClassName)('SelectValue'),
        ],
    });
    return `
  ${imports.generate()}

  export interface Variant {
    id: ${enum_.types.name};
    label: string;
  }

  export const variants = [ ${variants} ] as Variant[]

  export interface ${enum_.types.name}SelectProps
    extends UnionOmit<
      React.ComponentPropsWithoutRef<typeof Select>,
      'value' | 'onValueChange'
    > {
    value?: ${enum_.types.name} | null;
    defaultValue?: ${enum_.types.name};
    label?: (variant: Variant) => string;
    item?: (variant: Variant) => React.ReactNode;

    onValueChange: (value: ${enum_.types.name}) => void;
  }

  export function ${enum_.types.name}Select(
    { value, label, item = label, children, ...delegated }: ${enum_.types.name}SelectProps,
  ) {
    const selected = useMemo(() => variants.find((variant) => variant.id === value), [value])

    return (
      <Select {...delegated} value={value ?? undefined}>
        {children ?? (
          <SelectTrigger>
            {selected && label ? label(selected) : <SelectValue />}
          </SelectTrigger>
        )}

        <SelectContent>
          {variants.map((variant) => (
            <SelectItem key={variant.id} value={variant.id}>
              {item?.(variant) ?? variant.label}
            </SelectItem>
          ))}
        </SelectContent>
      </Select>
    )
  }
  `;
}
//# sourceMappingURL=inputs.generator.js.map