import { writeFile } from './chunk-V3VWCAQH.js';
import { __name } from './chunk-6AXVLTU5.js';
import { LogLevelLabel } from '@storm-software/config-tools/types';
import { createDirectory } from '@stryke/fs/helpers';
import { throttle } from '@stryke/helpers/throttle';
import { StormJSON } from '@stryke/json/storm-json';
import { joinPaths, existsSync, findFilePath } from '@stryke/path/index';
import { createUnimport as createUnimport$1 } from 'unimport';

var lastImportsDump;
function createUnimport(log, context) {
  log(LogLevelLabel.TRACE, "Creating Unimport context with Storm Stack presets");
  const unimport = createUnimport$1({
    commentsDisable: [
      "@unimport-disable",
      "@imports-disable",
      "@storm-disable",
      "@storm-ignore"
    ],
    commentsDebug: [
      "@unimport-debug",
      "@imports-debug",
      "@storm-debug"
    ],
    injectAtEnd: true,
    presets: context.unimportPresets
  });
  async function dumpImports() {
    log(LogLevelLabel.TRACE, "Dumping import file...");
    const items = await unimport.getImports();
    const importDumpFile = joinPaths(context.projectRoot, context.artifactsDir, "imports-dump.json");
    if (!existsSync(findFilePath(importDumpFile))) {
      await createDirectory(findFilePath(importDumpFile));
    }
    log(LogLevelLabel.TRACE, `Writing imports-dump JSON file: ${importDumpFile}`);
    const content = StormJSON.stringify(items);
    if (content.trim() !== lastImportsDump?.trim()) {
      lastImportsDump = content;
      await writeFile(log, importDumpFile, content);
    }
  }
  __name(dumpImports, "dumpImports");
  const dumpImportsThrottled = throttle(dumpImports, 500);
  async function injectImports(source) {
    await unimport.injectImports(source.code, source.id);
    if (!source.code.hasChanged()) {
      return source;
    }
    await dumpImportsThrottled();
    return source;
  }
  __name(injectImports, "injectImports");
  return {
    ...unimport,
    dumpImports: dumpImportsThrottled,
    injectImports
  };
}
__name(createUnimport, "createUnimport");

export { createUnimport };
//# sourceMappingURL=chunk-RWOBQPSN.js.map
//# sourceMappingURL=chunk-RWOBQPSN.js.map