import { weakMemoize } from '@takeshape/util';
import { createSchemaPropertyAccessor } from "./schema-util.js";
export function getFirstStringFieldV3(shape, projectSchema) {
    const forms = projectSchema.forms?.[shape.name];
    if (!forms?.default) {
        return null;
    }
    const schemaProperties = createSchemaPropertyAccessor(projectSchema, shape);
    const { order } = forms.default;
    const name = order.find((name) => schemaProperties.getValue(name)?.type === 'string');
    return name ? { name, key: schemaProperties.getKey(name) } : null;
}
export const getTaxonomyField = weakMemoize((shape, projectSchema) => shape.model?.type === 'taxonomy' ? getFirstStringFieldV3(shape, projectSchema) : null);
export const getTaxonomies = weakMemoize((projectSchema) => {
    const { shapes } = projectSchema;
    return Object.keys(shapes).reduce((result, shapeName) => {
        const shape = shapes[shapeName];
        const field = getTaxonomyField(shape, projectSchema);
        if (field) {
            result.push({ shapeId: shape.id, field });
        }
        return result;
    }, []);
});
