import { CollectionTemplate } from "../../data-source/collection-template/CollectionTemplate.mjs";
import { i18n } from "../../i18n/index.mjs";
import { FieldsConfigure, PreviewTable, SQLInput, SQLRequestProvider } from "./components/sql-collection/index.mjs";
import { getConfigurableProperties } 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 SqlCollectionTemplate extends CollectionTemplate {
    constructor(...args){
        super(...args), _define_property(this, "name", 'sql'), _define_property(this, "title", '{{t("SQL collection")}}'), _define_property(this, "order", 4), _define_property(this, "color", 'yellow'), _define_property(this, "default", {
            fields: []
        }), _define_property(this, "configurableProperties", {
            title: {
                type: 'string',
                title: '{{ t("Collection display name") }}',
                required: true,
                'x-decorator': 'FormItem',
                'x-component': 'Input'
            },
            name: {
                type: 'string',
                title: '{{t("Collection name")}}',
                required: true,
                'x-disabled': '{{ !createOnly }}',
                'x-decorator': 'FormItem',
                'x-component': 'Input',
                'x-validator': 'uid',
                description: "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}"
            },
            config: {
                type: 'void',
                'x-decorator': SQLRequestProvider,
                properties: {
                    sql: {
                        type: 'string',
                        title: '{{t("SQL")}}',
                        'x-decorator': 'FormItem',
                        'x-component': SQLInput,
                        required: true,
                        'x-validator': (value, rules, param)=>{
                            let { form } = param;
                            const field = form.query('sql').take();
                            if (!field.componentProps.disabled) return i18n.t('Please confirm the SQL statement first');
                            return '';
                        }
                    },
                    sources: {
                        type: 'array',
                        title: '{{t("Source collections")}}',
                        'x-decorator': 'FormItem',
                        'x-component': 'Select',
                        'x-component-props': {
                            multiple: true
                        },
                        'x-reactions': [
                            '{{useAsyncDataSource(loadCollections)}}'
                        ]
                    },
                    fields: {
                        type: 'array',
                        title: '{{t("Fields")}}',
                        'x-decorator': 'FormItem',
                        'x-component': FieldsConfigure,
                        required: true
                    },
                    table: {
                        type: 'void',
                        title: '{{t("Preview")}}',
                        'x-decorator': 'FormItem',
                        'x-component': PreviewTable
                    }
                }
            },
            ...getConfigurableProperties('category')
        });
    }
}
export { SqlCollectionTemplate };
