const fs=require("fs"),path=require("path"),appConfig=require("./config"),INDEX_FILE="index.json";function getIndexPath(){const cacheDir=path.join(appConfig.getBGXiongDataPath(),"sfx_downloads");return path.join(cacheDir,INDEX_FILE)}function ensureCacheDir(){const cacheDir=path.join(appConfig.getBGXiongDataPath(),"sfx_downloads");return fs.existsSync(cacheDir)||fs.mkdirSync(cacheDir,{recursive:!0}),cacheDir}function readIndex(){try{const indexPath=getIndexPath();if(!fs.existsSync(indexPath))return{};const data=fs.readFileSync(indexPath,"utf8");return JSON.parse(data)}catch(e){return{}}}function writeIndex(index){try{ensureCacheDir();const indexPath=getIndexPath();return fs.writeFileSync(indexPath,JSON.stringify(index,null,2),"utf8"),!0}catch(e){return!1}}function addToIndex(fileHash,filename){const index=readIndex();index[fileHash]={filename:filename,downloadedAt:Date.now()};return writeIndex(index)}function removeFromIndex(fileHash){const index=readIndex();return!index[fileHash]||(delete index[fileHash],writeIndex(index))}function getIndex(){return readIndex()}function hasFile(fileHash){return!!readIndex()[fileHash]}function clearIndex(){return writeIndex({})}function getFileUrl(fileHash){const fileInfo=readIndex()[fileHash];if(!fileInfo)return null;const cacheDir=path.join(appConfig.getBGXiongDataPath(),"sfx_downloads"),normalizedPath=path.join(cacheDir,fileInfo.filename).replace(/\\/g,"/");return"file://"+(normalizedPath.startsWith("/")?normalizedPath:"/"+normalizedPath)}module.exports={addToIndex:addToIndex,removeFromIndex:removeFromIndex,getIndex:getIndex,hasFile:hasFile,clearIndex:clearIndex,readIndex:readIndex,writeIndex:writeIndex,getFileUrl:getFileUrl};