"use client";
import { jsx as _jsx } from "react/jsx-runtime";
import { useEditContext } from "../client/editContext";
import { useEffect, useState, useCallback } from "react";
import { getLookupSources } from "../services/editService";
import { Select } from "../../components";
export function DropListEditor({ field, readOnly, }) {
    const editContext = useEditContext();
    const [lazyItems, setLazyItems] = useState([]);
    const [lazyLoading, setLazyLoading] = useState(true);
    if (!field)
        return;
    if (!editContext)
        return;
    useEffect(() => {
        onLazyLoad();
    }, [field.descriptor]);
    const onLazyLoad = useCallback(async () => {
        setLazyLoading(true);
        const response = await getLookupSources(field);
        const options = response.items;
        if (field.value && !options.find((o) => o.name === field.value)) {
            console.log("Adding unknown option", field.value);
            options.unshift({
                id: "unknown",
                name: `[Unknown: ${field.value}]`,
            });
        }
        setLazyItems(options);
        setLazyLoading(false);
    }, [field, editContext.sessionId]);
    // Convert ItemIdAndName[] to SelectOption[] using name for both value and label
    const selectOptions = lazyItems.map((item) => ({
        value: item.name,
        label: item.name,
    }));
    // Find the current value or show unknown value
    const currentValue = lazyItems.find((o) => o.name === field.value)?.name || field.value;
    return (_jsx(Select, { value: currentValue, onValueChange: (value) => {
            editContext?.operations.editField({
                field: field.descriptor,
                rawValue: value,
            });
        }, options: selectOptions, placeholder: "Select", disabled: readOnly, className: "md:w-14rem w-full", loading: lazyLoading, onLazyLoad: onLazyLoad, emptyMessage: "No options available", searchable: true }));
}
//# sourceMappingURL=DropListEditor.js.map