'use strict';
import { promises, existsSync } from 'fs';
import { loadMetaJson } from '../../meta/server/resources/init-internal-resource-metas-from-json.js';
import { onDeleteActions } from '../../meta/server/meta-core/constants/db-schema-actions.js';
import { propertyTypes } from '../../meta/properties/property-type.js';
import { withIdProps } from '../../../fd-toolbox-core/types/resource-with-id.js';
import { wellKnownMetaProps } from '../../../fd-toolbox-core/constants/meta-constants.js';
import { convertToKebabCase } from '../../../fd-toolbox/server/strings/casing.js';
import { convertFromPascalToCamelCase } from '../../../fd-toolbox/strings/casing-converter.js';
import { getObjectValues } from '../../../fd-toolbox/lib/utils.js';
import { generatedPaths } from '../../../base-repo/app-constants/project-paths.js';
import { SchemaAdapter } from '../../meta/server/db/schema/schema-adapter.js';
import { getDbType } from '../../db/db-config.js';
import { fileBaseNames } from '../../../base-repo/constants/internal-common-strings.js';

var __defProp=Object['defineProperty'],__name=(_0x4d521d,_0x47b271)=>__defProp(_0x4d521d,'name',{'value':_0x47b271,'configurable':!![]});const baseResourceColumnsPrefixes={'createdBy':'CreatedBy','modifiedBy':'ModifiedBy'},updateDeleteActions={'cascade':'cascade','restricted':'restrict','noAction':'no\x20action','setNull':'set\x20null','setDefault':'set\x20default'},schemaAdapter=new SchemaAdapter(getDbType());async function generateSchema(_0xbb6aaa){const _0x3ab383=generatedPaths['schema'],_0x3242fe=_0xbb6aaa??_0x3ab383,_0x26606a=new Set(),_0x409e51=[],_0x15e1e8=loadMetaJson();for(const _0x102440 in _0x15e1e8){const _0x4ae5d0=_0x15e1e8[_0x102440];if(!_0x4ae5d0['transientResource']&&!_0x4ae5d0['extendedFrom']){const _0x2c19fe=generateTable(_0x4ae5d0,_0x15e1e8);_0x2c19fe['methods']['forEach'](_0xe6a6b0=>_0x26606a['add'](_0xe6a6b0)),_0x409e51['push'](..._0x2c19fe['tableRows']['map'](_0x38a39e=>''+_0x38a39e)),_0x409e51['push'](..._0x2c19fe['indexes']);}}const _0x3d8fa7=schemaAdapter['getSchemaPackageName'](),_0x38f0c9='import\x20{\x20'+Array['from'](_0x26606a)['join'](',\x20')+'\x20}\x20from\x20\x22'+_0x3d8fa7+'\x22',_0x1ce6bf=[_0x38f0c9,'',..._0x409e51]['join']('\x0a');await checkOrCreateDirectory(_0x3242fe['substring'](-0x3b*-0x88+0x5fb*-0x6+-0x7*-0xa6,_0x3242fe['lastIndexOf']('/'))),await promises['writeFile'](_0x3242fe,_0x1ce6bf);}__name(generateSchema,'generateSchema');function generateRow(_0x19ea25,_0x3e8f45,_0x47ec34){const _0x43eb0d=schemaAdapter['getSchemaColumnDefinition'](_0x19ea25),_0x37bfa1={'drizzleImports':_0x43eb0d['imports'],'propertyRow':'\x20\x20\x20\x20'+_0x19ea25['name']+':\x20'+_0x43eb0d['definition']};return _0x19ea25['name']===wellKnownMetaProps['id']?_0x37bfa1['propertyRow']+='.primaryKey()':(_0x19ea25['required']&&(_0x37bfa1['propertyRow']+='.notNull()'),_0x19ea25['name']['slice'](-(-0x13f9*-0x1+0xd3f+-0x2136))['toLowerCase']()===wellKnownMetaProps['id']['toLowerCase']()&&addRelatedSchemaToReferences(_0x19ea25,_0x3e8f45,_0x47ec34,_0x37bfa1),_0x19ea25['unique']&&(_0x37bfa1['propertyRow']+='.unique()')),_0x37bfa1['propertyRow']+=',',_0x37bfa1;}__name(generateRow,'generateRow');function addRelatedSchemaToReferences(_0xc9f917,_0x8ffbe8,_0x58038a,_0x17053b){const _0x3e8fe4=_0xc9f917['name']['slice'](-0x1633+-0x4f*-0x69+-0xa34,-(-0x78d*-0x1+-0x8de+0x153))['replace'](new RegExp(getObjectValues(baseResourceColumnsPrefixes)['join']('|'),'gi'),''),_0x30a4da=getObjectValues(_0x58038a)['find'](_0x8975ea=>_0x8975ea['name']===_0x8ffbe8['name']);if(_0x30a4da){const _0x1c6fb1=_0x30a4da['properties'][_0x3e8fe4];_0x1c6fb1?.['declaredWithType']&&addReferences(_0x1c6fb1,_0x8ffbe8,_0x17053b,_0xc9f917);}}__name(addRelatedSchemaToReferences,'addRelatedSchemaToReferences');function getOnDeleteAction(_0x2dbcec){switch(_0x2dbcec){case onDeleteActions['cascade']:return updateDeleteActions['cascade'];case onDeleteActions['setNull']:return updateDeleteActions['setNull'];case onDeleteActions['restrict']:return updateDeleteActions['restricted'];default:return '';}}__name(getOnDeleteAction,'getOnDeleteAction');function addReferences(_0x1b5c0e,_0x1c7099,_0x332848,_0x2f39ba){if(_0x1b5c0e['declaredWithType']){const _0x4c99eb=convertFromPascalToCamelCase(_0x1b5c0e['declaredWithType'])+'Schema',_0x30db7f=convertToKebabCase(_0x4c99eb),_0x3299ea=_0x1b5c0e['declaredWithType']===_0x1c7099['name'],_0x3bb348=schemaAdapter['getSchemaAnyColumnType'](),_0x5d6fa0=_0x3299ea?':\x20'+_0x3bb348:'';_0x332848['propertyRow']+='.references(()'+_0x5d6fa0+'\x20=>\x20'+_0x4c99eb+'.id';const _0x5dbeab=_0x2f39ba['required']?onDeleteActions['cascade']:onDeleteActions['setNull'],_0x13ddd5=_0x2f39ba['onDelete']??_0x5dbeab;_0x332848['propertyRow']+=',\x20{\x20onDelete:\x20\x22'+getOnDeleteAction(_0x13ddd5)+'\x22\x20}',_0x332848['propertyRow']+=')',_0x3299ea&&_0x332848['drizzleImports']['push'](_0x3bb348),_0x1b5c0e['declaredWithType']!==_0x1c7099['name']&&(_0x332848['usedImport']='import\x20{\x20'+_0x4c99eb+'\x20}\x20from\x20\x22./'+_0x30db7f+'\x22');}}__name(addReferences,'addReferences');function generateIndexes(_0x5918f1){const _0x5e79ba=[],_0x31e9af=_0x5918f1['persistence']['tableName'],_0x10e79c=Object['values'](_0x5918f1['properties']),_0x1d3c28=_0x10e79c['some'](_0x5cbead=>_0x5cbead['name']===wellKnownMetaProps['workspaceId']),_0x2ff0a5=_0x10e79c['filter'](_0x4ae7bd=>_0x4ae7bd['isIndexed']);for(const _0x2899e1 of _0x2ff0a5){const _0x29fd8f=_0x1d3c28?_0x31e9af+'_workspace_id_'+_0x2899e1['name']+'_idx':_0x31e9af+'_'+_0x2899e1['name']+'_idx';_0x1d3c28?_0x5e79ba['push']('\x20\x20\x20\x20index(\x22'+_0x29fd8f+'\x22).on(table.workspaceId,\x20table.'+_0x2899e1['name']+'),'):_0x5e79ba['push']('\x20\x20\x20\x20index(\x22'+_0x29fd8f+'\x22).on(table.'+_0x2899e1['name']+'),');}return _0x5e79ba;}__name(generateIndexes,'generateIndexes');function generateTable(_0x300b99,_0xd12780){const _0x20b166=convertFromPascalToCamelCase(_0x300b99['name']),_0x534bb0=_0x300b99['persistence']['tableName'],_0x58a584=schemaAdapter['getSchemaTableConstructor'](),_0x56e592={'tableRows':['export\x20const\x20'+_0x20b166+'Schema\x20=\x20'+_0x58a584+'(\x22'+_0x534bb0+'\x22,\x20{'],'imports':new Set(),'methods':new Set(),'indexes':[]};_0x56e592['methods']['add'](_0x58a584);const _0x8defe7=getObjectValues(_0x300b99['properties'])['sort']((_0x3c1771,_0x115302)=>{let _0x273572=0x3*-0x3b7+0x33*-0x2b+0x9db*0x2;return _0x273572===0x14b*-0x13+-0x1*-0x1085+0x1*0x80c&&_0x3c1771['name']===withIdProps['id']&&(_0x273572=-(-0x1433*0x1+-0x1642*-0x1+0x107*-0x2)),_0x273572===0x19c5+-0x2*0xa34+-0x55d&&_0x115302['name']===withIdProps['id']&&(_0x273572=-0x1*0xf75+0x1887+0xd3*-0xb),_0x273572===-0x1372+0x1352*-0x1+-0x4*-0x9b1&&_0x3c1771['type']===propertyTypes['uuid']&&_0x115302['type']!==propertyTypes['uuid']&&(_0x273572=0x1*0x14e3+0x1a0b+-0x2eed),_0x273572===0x22ba+0x1*0x151f+0x1*-0x37d9&&_0x115302['type']===propertyTypes['uuid']&&_0x3c1771['type']!==propertyTypes['uuid']&&(_0x273572=-(0xbaf*-0x1+0x1*0xcd8+-0x94*0x2)),_0x273572;});for(const _0x5c1c0d of _0x8defe7){if(!_0x5c1c0d['isNavigationProperty']&&!_0x5c1c0d['isDynamic']){const _0x4483ec=generateRow(_0x5c1c0d,_0x300b99,_0xd12780);_0x56e592['tableRows']['push'](_0x4483ec['propertyRow']),_0x4483ec['usedImport']&&_0x56e592['imports']['add'](_0x4483ec['usedImport']),_0x4483ec['drizzleImports']['forEach'](_0x265855=>_0x56e592['methods']['add'](_0x265855));}}return addIndexesSection(_0x56e592,_0x300b99),_0x56e592;}__name(generateTable,'generateTable');function addIndexesSection(_0x57c2a1,_0x1f20d3){const _0x2e4404=generateIndexes(_0x1f20d3);_0x2e4404['length']?(_0x57c2a1['tableRows']['push']('},\x20(table)\x20=>\x20['),_0x2e4404['forEach'](_0x587231=>_0x57c2a1['tableRows']['push'](_0x587231)),_0x57c2a1['tableRows']['push'](']);'),_0x57c2a1['methods']['add'](fileBaseNames['index'])):_0x57c2a1['tableRows']['push']('});');}__name(addIndexesSection,'addIndexesSection');async function checkOrCreateDirectory(_0x4ca9e0){!existsSync(_0x4ca9e0)&&await promises['mkdir'](_0x4ca9e0,{'recursive':!![]});}__name(checkOrCreateDirectory,'checkOrCreateDirectory');

export { generateSchema };
