'use strict';

var chunkUJJENY47_cjs = require('./chunk-UJJENY47.cjs');
var chunkAF3E2D32_cjs = require('./chunk-AF3E2D32.cjs');
var types = require('@storm-software/config-tools/types');
var helpers = require('@stryke/fs/helpers');
var throttle = require('@stryke/helpers/throttle');
var stormJson = require('@stryke/json/storm-json');
var index = require('@stryke/path/index');
var unimport = require('unimport');

var lastImportsDump;
function createUnimport(log, context) {
  log(types.LogLevelLabel.TRACE, "Creating Unimport context with Storm Stack presets");
  const unimport$1 = unimport.createUnimport({
    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(types.LogLevelLabel.TRACE, "Dumping import file...");
    const items = await unimport$1.getImports();
    const importDumpFile = index.joinPaths(context.projectRoot, context.artifactsDir, "imports-dump.json");
    if (!index.existsSync(index.findFilePath(importDumpFile))) {
      await helpers.createDirectory(index.findFilePath(importDumpFile));
    }
    log(types.LogLevelLabel.TRACE, `Writing imports-dump JSON file: ${importDumpFile}`);
    const content = stormJson.StormJSON.stringify(items);
    if (content.trim() !== lastImportsDump?.trim()) {
      lastImportsDump = content;
      await chunkUJJENY47_cjs.writeFile(log, importDumpFile, content);
    }
  }
  chunkAF3E2D32_cjs.__name(dumpImports, "dumpImports");
  const dumpImportsThrottled = throttle.throttle(dumpImports, 500);
  async function injectImports(source) {
    await unimport$1.injectImports(source.code, source.id);
    if (!source.code.hasChanged()) {
      return source;
    }
    await dumpImportsThrottled();
    return source;
  }
  chunkAF3E2D32_cjs.__name(injectImports, "injectImports");
  return {
    ...unimport$1,
    dumpImports: dumpImportsThrottled,
    injectImports
  };
}
chunkAF3E2D32_cjs.__name(createUnimport, "createUnimport");

exports.createUnimport = createUnimport;
//# sourceMappingURL=chunk-VC2OEBZN.cjs.map
//# sourceMappingURL=chunk-VC2OEBZN.cjs.map