'use strict';
import { spawn } from 'child_process';
import { logInfo, logError } from '../../../fd-toolbox/server/logging/logger.js';
import { streamEventNames } from '../../../base-repo/constants/internal-common-strings.js';
import { childProcessEvents } from '../../../fd-toolbox/constants/child-process-events.js';
import { toolboxEnvSettingKeys } from '../../../fd-toolbox/infra/toolbox-env-setting-keys.js';
import { dbErrorMessages } from '../constants/db-constants.js';
import { getBooleanEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';
import { createError } from '../../../fd-toolbox/errors/errors.js';
import { toolboxCoreEnvVars } from '../../../fd-toolbox-core/constants/environment-variables-constants.js';
import { fileExtensionsWithoutDot } from '../../../base-repo/app-constants/internal-system-constants.js';
import { stdioStatuses } from '../../../base-repo/constants/tools-constants.js';
import { importTsx } from '../../../infra/ci/constants/ci-constants.js';

var __defProp=Object['defineProperty'],__name=(_0x42ca05,_0x57dcd5)=>__defProp(_0x42ca05,'name',{'value':_0x57dcd5,'configurable':!![]});const migrationStatusMessages={'generatingMigrations':'Generate\x20migrations...'};async function runDrizzleMigration(_0x1e8be7){logInfo('Pushing\x20schema\x20changes\x20for\x20'+_0x1e8be7['dialect']+'...');let _0x10b9d7;const _0x83f156='--config=drizzle.config.ts',_0x4098e1=getBooleanEnvSetting(toolboxEnvSettingKeys['useMigrations']);_0x4098e1?(_0x10b9d7='drizzle-kit\x20migrate\x20'+_0x83f156,logInfo('Running\x20migration\x20approach...')):_0x10b9d7='drizzle-kit\x20push\x20'+_0x83f156,await executeCommand(_0x10b9d7);}__name(runDrizzleMigration,'runDrizzleMigration');async function runDrizzleGeneration(_0x1dedcb){logInfo(migrationStatusMessages['generatingMigrations']);const _0x469b22='drizzle-kit\x20generate\x20--config=drizzle.config.ts\x20--name='+_0x1dedcb;await executeCommand(_0x469b22);}__name(runDrizzleGeneration,'runDrizzleGeneration');async function executeCommand(_0x35bcf7){return new Promise((_0x51867e,_0x3c015f)=>{const _0x281c7a={...process['env']};isCurrentProcessUsingTsx()&&!_0x281c7a[toolboxCoreEnvVars['nodeOptions']]?.['includes'](fileExtensionsWithoutDot['tsx'])&&(_0x281c7a[toolboxCoreEnvVars['nodeOptions']]=_0x281c7a[toolboxCoreEnvVars['nodeOptions']]?_0x281c7a[toolboxCoreEnvVars['nodeOptions']]+'\x20'+importTsx:importTsx);const _0xbb561b=spawn(_0x35bcf7,{'shell':!![],'stdio':[stdioStatuses['inherit'],stdioStatuses['inherit'],stdioStatuses['pipe']],'env':_0x281c7a});_0xbb561b['stderr']['on'](streamEventNames['data'],async _0x59df7d=>{const _0x174221=_0x59df7d['toString']();logError(_0x174221);}),_0xbb561b['on'](childProcessEvents['exit'],_0xc9d1fd=>{if(_0xc9d1fd===0x9*-0x33f+0xdb8+0xf7f)_0x51867e();else {const _0xce7e52=_0xc9d1fd?_0xc9d1fd['toString']():'No\x20Code';logError(dbErrorMessages['migrationErrorWithCode']['replace']('{0}',_0xce7e52)),_0x3c015f(createError(dbErrorMessages['migrationErrorWithCode']['replace']('{0}',_0xce7e52)));}}),_0xbb561b['on'](childProcessEvents['error'],_0x1c627a=>{logError(dbErrorMessages['migrationErrorWithMessage']['replace']('{0}',_0x1c627a['message'])),_0x3c015f(_0x1c627a);});});}__name(executeCommand,'executeCommand');function isCurrentProcessUsingTsx(){const _0x345aa0=process['execArgv']['some'](_0x3ac24d=>_0x3ac24d['includes'](fileExtensionsWithoutDot['tsx'])||_0x3ac24d['includes'](importTsx)),_0x46b873=process['execPath']['includes'](fileExtensionsWithoutDot['tsx']);return _0x345aa0||_0x46b873;}__name(isCurrentProcessUsingTsx,'isCurrentProcessUsingTsx');

export { migrationStatusMessages, runDrizzleGeneration, runDrizzleMigration };
