/** Fills source-neutral defaults over `input` and memoizes the load so the collection
 *  query and the table columns share a single fetch. */
export function defineSchemaSource(input) {
    const { collectionId, paginationMode = 'offset', creatableFieldTypes = [], fieldPermissions, supportsPii = false, } = input;
    let loaded;
    let schema;
    const loadSchema = () => (loaded ??= input
        .loadSchema()
        .then((result) => {
        schema = result.schema;
        return result;
    })
        .catch((error) => {
        loaded = undefined;
        throw error;
    }));
    const searchableFieldIds = input.searchableFieldIds ?? ((s) => [s.displayField]);
    const itemKey = input.itemKey ??
        ((item) => String(item[schema?.idField ?? '_id'] ?? ''));
    const itemName = input.itemName ??
        ((item) => String(item[schema?.displayField ?? '_id'] ?? ''));
    const canAddFields = input.canAddFields ?? ((s) => s.schemaPermissions?.createField ?? false);
    const canEditField = input.canEditField ??
        ((field, s) => (s.schemaPermissions?.updateField ?? false) && !field.isSystem);
    const canDeleteField = input.canDeleteField ??
        ((field, s) => (s.schemaPermissions?.deleteField ?? false) && !field.isSystem);
    const fetchData = async (query) => {
        const { backend, schema: loadedSchema } = await loadSchema();
        return backend.find(query, {
            searchableFieldIds: searchableFieldIds(loadedSchema),
        });
    };
    return {
        collectionId,
        loadSchema,
        collectionConfig: {
            queryName: collectionId,
            paginationMode,
            fetchData,
            itemKey,
            itemName,
        },
        creatableFieldTypes,
        searchableFieldIds,
        canAddFields,
        canEditField,
        canDeleteField,
        fieldPermissions,
        supportsPii,
    };
}
//# sourceMappingURL=SchemaSource.js.map