import { CollectionFieldInterface } from "../../data-source/collection-field-interface/CollectionFieldInterface.mjs";
import { constraintsProps, operators, relationshipType, reverseFieldProperties } from "./properties/index.mjs";
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
class O2MFieldInterface extends CollectionFieldInterface {
    schemaInitialize(schema, param) {
        let { field, block, readPretty, targetCollection } = param;
        var _schema_xcomponentprops_fieldNames;
        schema['x-component-props'] = schema['x-component-props'] || {};
        schema['x-component-props'].fieldNames = schema['x-component-props'].fieldNames || {
            value: getUniqueKeyFromCollection(targetCollection)
        };
        schema['x-component-props'].fieldNames.label = (null == (_schema_xcomponentprops_fieldNames = schema['x-component-props'].fieldNames) ? void 0 : _schema_xcomponentprops_fieldNames.label) || (null == targetCollection ? void 0 : targetCollection.titleField) || getUniqueKeyFromCollection(targetCollection);
        if ([
            'Table',
            'Kanban'
        ].includes(block)) {
            schema['x-component-props'] = schema['x-component-props'] || {};
            schema['x-component-props']['ellipsis'] = true;
            schema['x-component-props']['size'] = 'small';
        }
    }
    constructor(...args){
        super(...args), _define_property(this, "name", 'o2m'), _define_property(this, "type", 'object'), _define_property(this, "group", 'relation'), _define_property(this, "order", 4), _define_property(this, "title", '{{t("One to many")}}'), _define_property(this, "icon", 'DeploymentUnitOutlined'), _define_property(this, "description", '{{t("One to many description")}}'), _define_property(this, "isAssociation", true), _define_property(this, "default", {
            type: 'hasMany',
            uiSchema: {
                'x-component': 'AssociationField',
                'x-component-props': {
                    multiple: true
                }
            },
            reverseField: {
                interface: 'm2o',
                type: 'belongsTo',
                uiSchema: {
                    'x-component': 'AssociationField',
                    'x-component-props': {
                        multiple: false
                    }
                }
            }
        }), _define_property(this, "availableTypes", [
            'hasMany'
        ]), _define_property(this, "properties", {
            'uiSchema.title': {
                type: 'string',
                title: '{{t("Field display name")}}',
                required: true,
                'x-decorator': 'FormItem',
                'x-component': 'Input'
            },
            name: {
                type: 'string',
                title: '{{t("Field name")}}',
                required: true,
                'x-disabled': '{{ !createOnly }}',
                'x-decorator': 'FormItem',
                'x-component': 'Input',
                description: "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}"
            },
            type: relationshipType,
            grid: {
                type: 'void',
                'x-component': 'Grid',
                properties: {
                    row1: {
                        type: 'void',
                        'x-component': 'Grid.Row',
                        properties: {
                            col11: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {
                                    source: {
                                        type: 'void',
                                        title: '{{t("Source collection")}}',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'SourceCollection'
                                    }
                                }
                            },
                            col12: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {
                                    target: {
                                        type: 'string',
                                        title: '{{t("Target collection")}}',
                                        required: true,
                                        'x-reactions': [
                                            '{{useAsyncDataSource(loadCollections, ["file"])}}'
                                        ],
                                        'x-decorator': 'FormItem',
                                        'x-component': 'Select',
                                        'x-disabled': '{{ !createOnly }}'
                                    }
                                }
                            }
                        }
                    },
                    row2: {
                        type: 'void',
                        'x-component': 'Grid.Row',
                        properties: {
                            col21: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {
                                    sourceKey: {
                                        type: 'string',
                                        title: '{{t("Source key")}}',
                                        description: "{{t('Field values must be unique.')}}",
                                        default: 'id',
                                        enum: [
                                            {
                                                label: 'ID',
                                                value: 'id'
                                            }
                                        ],
                                        'x-decorator': 'FormItem',
                                        'x-component': 'SourceKey'
                                    }
                                }
                            },
                            col22: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {
                                    foreignKey: {
                                        type: 'string',
                                        title: '{{t("Foreign key")}}',
                                        required: true,
                                        default: '{{ useNewId("f_") }}',
                                        description: "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}",
                                        'x-decorator': 'FormItem',
                                        'x-component': 'ForeignKey',
                                        'x-validator': 'uid',
                                        'x-disabled': '{{ !createOnly }}'
                                    }
                                }
                            }
                        }
                    },
                    row3: {
                        type: 'void',
                        'x-component': 'Grid.Row',
                        properties: {
                            col21: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {}
                            },
                            col22: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {
                                    targetKey: {
                                        type: 'string',
                                        title: '{{t("Target key")}}',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'TargetKey',
                                        'x-disabled': '{{ !createOnly }}',
                                        description: "{{t('Field values must be unique.')}}"
                                    }
                                }
                            }
                        }
                    }
                }
            },
            ...constraintsProps,
            ...reverseFieldProperties
        }), _define_property(this, "filterable", {
            nested: true,
            operators: operators.collectionField,
            children: []
        });
    }
}
function getUniqueKeyFromCollection(collection) {
    return (null == collection ? void 0 : collection.filterTargetKey) || (null == collection ? void 0 : collection.getPrimaryKey()) || 'id';
}
export { O2MFieldInterface, getUniqueKeyFromCollection };
