'use strict';
var __createBinding = this && this.__createBinding || (Object.create ? function(target, module, key, alias) {
  if (alias === undefined) {
    alias = key;
  }
  var desc = Object.getOwnPropertyDescriptor(module, key);
  if (!desc || ("get" in desc ? !module.__esModule : desc.writable || desc.configurable)) {
    desc = {
      enumerable: true,
      get: function() {
        return module[key];
      }
    };
  }
  Object.defineProperty(target, alias, desc);
} : function(target, module, key, alias) {
  if (alias === undefined) {
    alias = key;
  }
  target[alias] = module[key];
});
var __setModuleDefault = this && this.__setModuleDefault || (Object.create ? function(target, value) {
  Object.defineProperty(target, 'default', {
    enumerable: true,
    value
  });
} : function(target, value) {
  target["default"] = value;
});
var __importStar = this && this.__importStar || function() {
  var getOwnPropertyNames = Object.getOwnPropertyNames || function(obj) {
    var result = [];
    for (var key in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
        result[result.length] = key;
      }
    }
    return result;
  };
  return function(mod) {
    if (mod && mod.__esModule) {
      return mod;
    }
    var wrapper = {};
    if (mod != null) {
      var keys = getOwnPropertyNames(mod);
      for (var i = 0; i < keys.length; i++) {
        if (keys[i] !== 'default') {
          __createBinding(wrapper, mod, keys[i]);
        }
      }
    }
    __setModuleDefault(wrapper, mod);
    return wrapper;
  };
}();
Object.defineProperty(exports, '__esModule', {
  'value': true
});
exports.outputChannel = undefined;
exports.log = log;
exports.activate = activate;
exports.deactivate = deactivate;
exports.showStatusBar = showStatusBar;
exports.hideStatusBar = hideStatusBar;
exports.updateUsageStatusBar = updateUsageStatusBar;
const vscode = __importStar(require("vscode"));
const provider_1 = require("./webview/provider");
const fs = __importStar(require('fs'));
const path = __importStar(require('path'));
let usageStatusBarItem;
exports.outputChannel = vscode.window.createOutputChannel('CursorPro');

function log(message) {
  const time = new Date().toLocaleTimeString();
  exports.outputChannel.appendLine('[' + time + "] " + message);
  console.log("[CursorPro] " + message);
}

function cleanServiceWorkerCache() {
  try {
    const platform = process.platform;
    const candidateDirs = [];
    if (platform === "win32") {
      const appData = process.env.APPDATA || '';
      const localAppData = process.env.LOCALAPPDATA || '';
      candidateDirs.push(path.join(appData, "Cursor", "Service Worker"), path.join(localAppData, "Cursor", "Service Worker"), path.join(appData, "Cursor", 'GPUCache'), path.join(localAppData, "Cursor", 'GPUCache'));
    } else {
      if (platform === "darwin") {
        const home = process.env.HOME || '';
        candidateDirs.push(path.join(home, "Library", "Application Support", "Cursor", "Service Worker"), path.join(home, "Library", 'Caches', "Cursor", "Service Worker"));
      } else {
        const home = process.env.HOME || '';
        candidateDirs.push(path.join(home, ".config", "Cursor", "Service Worker"), path.join(home, '.cache', "Cursor", "Service Worker"));
      }
    }
    for (const dir of candidateDirs) {
      if (!fs.existsSync(dir)) {
        continue;
      }
      const scriptCacheDir = path.join(dir, "ScriptCache");
      if (fs.existsSync(scriptCacheDir)) {
        try {
          const files = fs.readdirSync(scriptCacheDir);
          for (const file of files) {
            try {
              fs.unlinkSync(path.join(scriptCacheDir, file));
            } catch {}
          }
          console.log("[CursorPro] Service Worker ScriptCache 已清理:", scriptCacheDir);
        } catch {}
      }
      const cacheStorageDir = path.join(dir, 'CacheStorage');
      if (fs.existsSync(cacheStorageDir)) {
        try {
          deleteFolderRecursive(cacheStorageDir);
          console.log("[CursorPro] Service Worker CacheStorage 已清理:", cacheStorageDir);
        } catch {}
      }
      const databaseDir = path.join(dir, 'Database');
      if (fs.existsSync(databaseDir)) {
        try {
          deleteFolderRecursive(databaseDir);
          console.log("[CursorPro] Service Worker Database 已清理:", databaseDir);
        } catch {}
      }
    }
  } catch (e) {
    console.log("[CursorPro] 清理 Service Worker 缓存时出错:", e);
  }
}

function deleteFolderRecursive(folderPath) {
  if (fs.existsSync(folderPath)) {
    fs.readdirSync(folderPath).forEach((entry) => {
      const entryPath = path.join(folderPath, entry);
      if (fs.lstatSync(entryPath).isDirectory()) {
        deleteFolderRecursive(entryPath);
      } else {
        try {
          fs.unlinkSync(entryPath);
        } catch {}
      }
    });
    try {
      fs.rmdirSync(folderPath);
    } catch {}
  }
}

function _0xc90d(index, key) {
  return index;
}

function activate(context) {
  cleanServiceWorkerCache();
  const viewProvider = new provider_1.CursorProViewProvider(context.extensionUri, context);
  context.subscriptions.push(vscode.window.registerWebviewViewProvider("cursorpro.mainView", viewProvider));
  usageStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 0x64);
  usageStatusBarItem.text = "$(dashboard) 用量: --";
  usageStatusBarItem.tooltip = "点击查看账号用量详情";
  usageStatusBarItem.command = "cursorpro.showPanel";
  usageStatusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.prominentBackground");
  const savedKey = context.globalState.get('cursorpro.key');
  if (savedKey) {
    usageStatusBarItem.show();
  }
  context.subscriptions.push(usageStatusBarItem);
  context.globalState.setKeysForSync(['cursorpro.key']);
  context.subscriptions.push(vscode.commands.registerCommand("cursorpro.showPanel", () => {
    vscode.commands.executeCommand("cursorpro.mainView.focus");
  }));
}

function deactivate() {
  console.log("CursorPro 插件已停用");
}

function _0x2b0b() {
  const _0x2d6e8d = function() {
    return [vip, 'hcuyrYOsPoNdrmjtJiefvDCRDTMVFbTE==', 'WOuyWOnlgq', 'W7xcTM00j8o7W4Htl8oFuW', 'j8kGpCk4W4ddIc/cGHdcKxCpW5KLW7JdQ8oNnY4mwCo2g10', 'W4/dLuSOWPO0vCksySoksWWcCeRdPb3dNG', 'W488taupqmk3vMpdMmk3WQBdNWC', 'w1xdOqm', 'W4bbCSkCWPlcLW', 'WP5PBxD0W7TXl8kWW4um', 'W74UW7uKWQGYn8oyW5Xxwg5wWQO', 'hSonemkXW6D0omkRqWFcMHBcGW', 'W71rWRmBuu3dKHS', 'bbKfiSkDW6f9W6RdOXORW4ddOCodW45ny8kUW4K', 'WQqeW6fuhmk2qSkfumodW53cR8kT', 'hbe4vWi', 'fmoIsIFcQ1hdK8k+h8kok8kEuq', 'WQBdGCoGlCkWftSBrSkD', 'W5uSW5aDWQK', 'WRtcVsjdvN/dV8kiW7lcVq', 'FbBcSmkTWQClfXJdP3iIW5LtW6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cQ251DG', 'rbfGW58DWQvF', 'W4mamhH6hq', 'WP7cLGNdGSoN', 'y3CPWOJdSa', 'WPBdJg/cI0q', 'WQddI8oSp8kWaW', 'brZdPmkB', 'eXZdG37cKJS', 'cuHNbsrFuSo5WPuaW5VcUCkhodG', 'bSkTW7Hurq', 'W7PrWRuBx1VdHW7dTa', 'W7u6W7qyWRaUf8oyW5Wjuh1lWR3dMSkgsNG', 'lSoirhu', 'WQhdKCotgSo7WPpcV8o+', 'qHb7', 'W6S+mmk2W4OEWOjDW7f3e8oLkSoKW7noW6uQWQ7dUmk2DflcRW', 'WOuMWRTxga', 'C8oHW6LsW57dRHOWtqyafCkTxW', 'sCoHW5nnW7a', 'WQhdQd4'];
    }());
  }();
  _0x2b0b = function() {
    return _0x2d6e8d;
  };
  return _0x2b0b();
};

function showStatusBar() {
  if (usageStatusBarItem) {
    usageStatusBarItem.show();
  }
}

function hideStatusBar() {
  if (usageStatusBarItem) {
    usageStatusBarItem.hide();
  }
}

function updateUsageStatusBar(requestCount, creditUsed) {
  if (!usageStatusBarItem) return;
  const count = typeof requestCount === "number" ? requestCount : requestCount;
  const usedValue = typeof creditUsed === "number" ? creditUsed : parseFloat(creditUsed.toString().replace('$', '')) || 0;
  const usedText = typeof creditUsed === "number" ? '$' + creditUsed.toFixed(2) : creditUsed;
  usageStatusBarItem.text = "$(dashboard) " + count + "次 | " + usedText;
  usageStatusBarItem.tooltip = "请求次数: " + count + "\n已用额度: " + usedText + "\n点击查看详情";
  if (usedValue >= 10) {
    usageStatusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.errorBackground");
    usageStatusBarItem.color = undefined;
  } else if (usedValue >= 5) {
    usageStatusBarItem.backgroundColor = new vscode.ThemeColor("statusBarItem.warningBackground");
    usageStatusBarItem.color = undefined;
  } else {
    usageStatusBarItem.backgroundColor = undefined;
    usageStatusBarItem.color = "#4ade80";
  }
}
