import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_collection_field_interface_CollectionFieldInterface_mjs__ from "../../data-source/collection-field-interface/CollectionFieldInterface.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__o2m_mjs__ from "./o2m.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__properties_index_mjs__ 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 M2MFieldInterface extends __WEBPACK_EXTERNAL_MODULE__data_source_collection_field_interface_CollectionFieldInterface_mjs__.CollectionFieldInterface {
    schemaInitialize(schema, param) {
        let { field, readPretty, block, 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: (0, __WEBPACK_EXTERNAL_MODULE__o2m_mjs__.getUniqueKeyFromCollection)(targetCollection)
        };
        schema['x-component-props'].fieldNames.label = (null === (_schema_xcomponentprops_fieldNames = schema['x-component-props'].fieldNames) || void 0 === _schema_xcomponentprops_fieldNames ? void 0 : _schema_xcomponentprops_fieldNames.label) || (null == targetCollection ? void 0 : targetCollection.titleField) || (0, __WEBPACK_EXTERNAL_MODULE__o2m_mjs__.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';
        }
    }
    initialize(values) {
        if ('belongsToMany' === values.type) {
            if (!values.through) values.through = "t_".concat((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
            if (!values.foreignKey) values.foreignKey = "f_".concat((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
            if (!values.otherKey) values.otherKey = "f_".concat((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.uid)());
            if (!values.sourceKey) values.sourceKey = 'id';
            if (!values.targetKey) values.targetKey = 'id';
        }
    }
    constructor(...args){
        super(...args), _define_property(this, "name", 'm2m'), _define_property(this, "type", 'object'), _define_property(this, "group", 'relation'), _define_property(this, "order", 6), _define_property(this, "title", '{{t("Many to many")}}'), _define_property(this, "description", '{{t("Many to many description")}}'), _define_property(this, "isAssociation", true), _define_property(this, "default", {
            type: 'belongsToMany',
            uiSchema: {
                'x-component': 'AssociationField',
                'x-component-props': {
                    multiple: true
                }
            },
            reverseField: {
                interface: 'm2m',
                type: 'belongsToMany',
                uiSchema: {
                    'x-component': 'AssociationField',
                    'x-component-props': {
                        multiple: true
                    }
                }
            }
        }), _define_property(this, "availableTypes", [
            'belongsToMany'
        ]), _define_property(this, "properties", {
            ...__WEBPACK_EXTERNAL_MODULE__properties_index_mjs__.defaultProps,
            type: __WEBPACK_EXTERNAL_MODULE__properties_index_mjs__.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: 'string',
                                        title: '{{t("Source collection")}}',
                                        'x-decorator': 'FormItem',
                                        'x-component': 'SourceCollection',
                                        'x-disabled': true
                                    }
                                }
                            },
                            col12: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {
                                    through: {
                                        type: 'string',
                                        title: '{{t("Through collection")}}',
                                        description: '{{ t("Generated automatically if left blank") }}',
                                        'x-decorator': 'FormItem',
                                        'x-disabled': '{{ !createOnly }}',
                                        'x-component': 'ThroughCollection',
                                        'x-component-props': {
                                            allowClear: true
                                        }
                                    }
                                }
                            },
                            col13: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {
                                    target: {
                                        type: 'string',
                                        title: '{{t("Target collection")}}',
                                        required: true,
                                        'x-reactions': [
                                            '{{useAsyncDataSource(loadCollections)}}'
                                        ],
                                        '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.')}}",
                                        'x-decorator': 'FormItem',
                                        'x-component': 'SourceKey',
                                        'x-disabled': '{{ !createOnly }}'
                                    }
                                }
                            },
                            col22: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {
                                    foreignKey: {
                                        type: 'string',
                                        title: '{{t("Foreign key 1")}}',
                                        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||override }}'
                                    }
                                }
                            },
                            col23: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {}
                            }
                        }
                    },
                    row3: {
                        type: 'void',
                        'x-component': 'Grid.Row',
                        properties: {
                            col21: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {}
                            },
                            col22: {
                                type: 'void',
                                'x-component': 'Grid.Col',
                                properties: {
                                    otherKey: {
                                        type: 'string',
                                        title: '{{t("Foreign key 2")}}',
                                        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||override }}'
                                    }
                                }
                            },
                            col23: {
                                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.')}}"
                                    }
                                }
                            }
                        }
                    }
                }
            },
            ...__WEBPACK_EXTERNAL_MODULE__properties_index_mjs__.reverseFieldProperties
        }), _define_property(this, "filterable", {
            nested: true,
            operators: __WEBPACK_EXTERNAL_MODULE__properties_index_mjs__.operators.collectionField,
            children: []
        });
    }
}
export { M2MFieldInterface };
