'use strict';

var chunkXSCSXEQC_cjs = require('./chunk-XSCSXEQC.cjs');
var chunkAF3E2D32_cjs = require('./chunk-AF3E2D32.cjs');
var chmodX = require('@stryke/fs/chmod-x');
var helpers = require('@stryke/fs/helpers');
var readFile = require('@stryke/fs/read-file');
var removeFile = require('@stryke/fs/remove-file');
var writeFile = require('@stryke/fs/write-file');
var hash = require('@stryke/hash');
var exists = require('@stryke/path/exists');
var filePathFns = require('@stryke/path/file-path-fns');
var joinPaths = require('@stryke/path/join-paths');

function getCacheHashKey(id, code) {
  return hash.hash({
    id,
    code: chunkXSCSXEQC_cjs.getString(code)
  }, {
    maxLength: 32
  });
}
chunkAF3E2D32_cjs.__name(getCacheHashKey, "getCacheHashKey");
function getCacheFileName(id, hashKey) {
  return `${filePathFns.findFileName(id, {
    withExtension: false
  })}_${hashKey}.cache`;
}
chunkAF3E2D32_cjs.__name(getCacheFileName, "getCacheFileName");
async function getCache(sourceFile, cacheDir) {
  const hashKey = getCacheHashKey(sourceFile.id, sourceFile.code);
  const cacheFilePath = joinPaths.joinPaths(cacheDir, getCacheFileName(sourceFile.id, hashKey));
  if (!exists.existsSync(cacheFilePath)) {
    return void 0;
  }
  const cache = await readFile.readFile(cacheFilePath);
  if (!cache.includes(hashComment(hashKey))) {
    return void 0;
  }
  return cache;
}
chunkAF3E2D32_cjs.__name(getCache, "getCache");
async function setCache(sourceFile, cacheDir, transpiled) {
  const hashKey = getCacheHashKey(sourceFile.id, sourceFile.code);
  const cacheFilePath = joinPaths.joinPaths(cacheDir, getCacheFileName(sourceFile.id, hashKey));
  if (exists.existsSync(cacheFilePath)) {
    await removeFile.removeFile(cacheFilePath);
  }
  if (transpiled) {
    if (!exists.existsSync(cacheDir)) {
      await helpers.createDirectory(cacheDir);
    }
    if (!await chmodX.isWritable(cacheDir)) {
      throw new Error(`Cache directory is not writable: ${cacheDir}`);
    }
    await writeFile.writeFile(cacheFilePath, `${transpiled}

${hashComment(hashKey)}`.trim());
  }
}
chunkAF3E2D32_cjs.__name(setCache, "setCache");
function hashComment(hashKey) {
  return `/* storm-stack_${hashKey} */`;
}
chunkAF3E2D32_cjs.__name(hashComment, "hashComment");

exports.getCache = getCache;
exports.setCache = setCache;
//# sourceMappingURL=chunk-NAJ36BYA.cjs.map
//# sourceMappingURL=chunk-NAJ36BYA.cjs.map