import * as path from 'node:path';
import type { ExternalAppConfig } from './config-loader';

export function resolveDataDir(input?: {
  isPkg?: boolean;
  execDir?: string;
  config?: Pick<ExternalAppConfig, 'data'>;
  cwd?: string;
}): string {
  if (input?.config?.data?.dir) return path.resolve(input.config.data.dir);
  if (process.env.NETA_DATA_DIR) return path.resolve(process.env.NETA_DATA_DIR);
  if (input?.isPkg ?? Boolean((process as any).pkg))
    return path.join(input?.execDir ?? path.dirname(process.execPath), 'data');
  return path.join(input?.cwd ?? process.cwd(), 'dist');
}
