import { jsx } from "react/jsx-runtime";
import "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { useCollectionManager } from "../data-source/index.mjs";
import { useDesignable } from "../schema-component/index.mjs";
import { SchemaSettingsSelectItem } from "./SchemaSettings.mjs";
const SchemaSettingCollection = ()=>{
    var _collections_dataSource_options;
    const fieldSchema = useFieldSchema();
    const collections = useCollectionManager();
    const field = useField();
    const options = null == collections ? void 0 : null == (_collections_dataSource_options = collections.dataSource['options']) ? void 0 : _collections_dataSource_options.collections.map((value)=>({
            label: value.title,
            value: value.name
        }));
    const { dn } = useDesignable();
    return /*#__PURE__*/ jsx(SchemaSettingsSelectItem, {
        title: "Edit Collection",
        options: options,
        value: fieldSchema['collectionName'],
        onChange: (name)=>{
            field.setValue('');
            const titleField = collections.getCollection(name).titleField;
            fieldSchema['collectionName'] = name;
            fieldSchema.default = '';
            fieldSchema['x-component-props'] = {
                ...fieldSchema['x-component-props'],
                collection: name,
                fieldNames: {
                    label: titleField,
                    value: titleField
                },
                defaultValue: ''
            };
            fieldSchema['x-decorator-props'] = name;
            field.componentProps = fieldSchema['x-component-props'];
            dn.emit('patch', {
                schema: {
                    ['x-uid']: fieldSchema['x-uid'],
                    collectionName: fieldSchema['collectionName'],
                    ['x-component-props']: fieldSchema['x-component-props'],
                    ['x-decorator-props']: fieldSchema['x-decorator-props'],
                    default: ''
                }
            });
            dn.refresh();
        }
    }, "component-field");
};
export { SchemaSettingCollection };
