"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAppStatsDataDirCandidates = getAppStatsDataDirCandidates;
exports.getAppStatsDataDir = getAppStatsDataDir;
exports.ensureAppStatsDataDir = ensureAppStatsDataDir;
exports.getAppStatsBinDir = getAppStatsBinDir;
exports.ensureAppStatsBinDir = ensureAppStatsBinDir;
exports.getBrowserExtensionDataDir = getBrowserExtensionDataDir;
exports.ensureBrowserExtensionDataDir = ensureBrowserExtensionDataDir;
exports.getBrowserExtensionConfigPath = getBrowserExtensionConfigPath;
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const APPSTATS_DATA_DIR_ENV = 'APPSTATS_DATA_DIR';
const dedupe = (values) => (Array.from(new Set(values.map((value) => String(value || '').trim()).filter(Boolean))));
function getAppStatsDataDirCandidates() {
    const configured = String(process.env[APPSTATS_DATA_DIR_ENV] || '').trim();
    if (process.platform === 'win32') {
        return dedupe([
            configured,
            path_1.default.join(String(process.env.PROGRAMDATA || process.env.ProgramData || 'C:\\ProgramData').trim() || 'C:\\ProgramData', 'AppStats'),
        ]);
    }
    const homeDir = String(process.env.HOME || os_1.default.homedir() || '').trim();
    return dedupe([
        configured,
        '/var/lib/appstats',
        '/opt/appstats',
        homeDir ? path_1.default.join(homeDir, '.local', 'share', 'appstats') : '',
        homeDir ? path_1.default.join(homeDir, '.config', 'appstats') : '',
    ]);
}
function getAppStatsDataDir() {
    const candidates = getAppStatsDataDirCandidates();
    const existing = candidates.find((candidate) => candidate && fs_1.default.existsSync(candidate));
    return existing || candidates[0] || path_1.default.join(os_1.default.homedir(), '.config', 'appstats');
}
function ensureAppStatsDataDir() {
    const configured = String(process.env[APPSTATS_DATA_DIR_ENV] || '').trim();
    if (configured) {
        fs_1.default.mkdirSync(configured, { recursive: true });
        return configured;
    }
    for (const candidate of getAppStatsDataDirCandidates()) {
        if (!candidate)
            continue;
        try {
            fs_1.default.mkdirSync(candidate, { recursive: true });
            process.env[APPSTATS_DATA_DIR_ENV] = candidate;
            return candidate;
        }
        catch {
            // try the next candidate
        }
    }
    throw new Error('Unable to create a writable AppStats data directory');
}
function getAppStatsBinDir() {
    return path_1.default.join(getAppStatsDataDir(), 'bin');
}
function ensureAppStatsBinDir() {
    const dir = path_1.default.join(ensureAppStatsDataDir(), 'bin');
    fs_1.default.mkdirSync(dir, { recursive: true });
    return dir;
}
function getBrowserExtensionDataDir() {
    return path_1.default.join(getAppStatsDataDir(), 'browser-extension');
}
function ensureBrowserExtensionDataDir() {
    const dir = path_1.default.join(ensureAppStatsDataDir(), 'browser-extension');
    fs_1.default.mkdirSync(dir, { recursive: true });
    return dir;
}
function getBrowserExtensionConfigPath() {
    return path_1.default.join(getBrowserExtensionDataDir(), 'unpacked', 'config.json');
}
//# sourceMappingURL=app-paths.js.map