import { ux } from "@oclif/core";
import fs from "fs-extra/esm";
import { existsSync } from "node:fs";
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { DEFAULT_CONFIG } from "../shared/constants.js";
export class ConfigurationService {
    _directoryConfig;
    get directoryConfig() {
        if (!this._directoryConfig) {
            throw new Error("ConfigurationService not initialized");
        }
        return this._directoryConfig;
    }
    set directoryConfig(config) {
        this._directoryConfig = config;
    }
    async getCliConfig() {
        const { configDir } = this.directoryConfig;
        const localConfigLocation = join(process.cwd(), "ioconnect.json");
        const localConfigDirExists = existsSync(localConfigLocation);
        if (localConfigDirExists) {
            return this.processConfig(localConfigLocation);
        }
        const envLicenseKey = process.env.IO_CONNECT_BROWSER_LICENSE_KEY;
        const envMode = process.env.IO_CONNECT_MODE;
        if (envLicenseKey && envLicenseKey.length > 0) {
            return {
                licenseKey: envLicenseKey,
                mode: this.parseMode(envMode)
            };
        }
        const globalConfigLocation = join(configDir, "config.json");
        const configDirExists = existsSync(globalConfigLocation);
        if (configDirExists) {
            return this.processConfig(globalConfigLocation);
        }
        return {
            mode: "auto"
        };
    }
    async getLicenseKey() {
        const config = await this.getCliConfig();
        return config.licenseKey;
    }
    async setCliConfig(config) {
        const { mode, token } = config;
        const { configDir } = this.directoryConfig;
        const configDirExists = existsSync(join(configDir));
        const configLocation = join(configDir, "config.json");
        if (!configDirExists) {
            await mkdir(join(configDir), { recursive: true });
        }
        const configFileExists = existsSync(configLocation);
        const configFile = configFileExists ? await fs.readJSON(configLocation, { throws: false }) : { ...DEFAULT_CONFIG };
        if (token) {
            configFile.licenseKey = token;
        }
        configFile.mode = mode;
        await writeFile(configLocation, JSON.stringify(configFile, null, 4));
    }
    parseMode(mode) {
        switch (mode) {
            case "auto":
            case "offline":
            case "remote": {
                return mode;
            }
            default: {
                ux.warn(`Invalid mode ${mode}, defaulting to auto`);
                return "auto";
            }
        }
    }
    async processConfig(configLocation) {
        const configFile = await fs.readJSON(configLocation, { throws: false });
        if (!configFile) {
            ux.error(`Could not find the config file at ${configLocation}`);
        }
        return {
            licenseKey: configFile.licenseKey,
            mode: this.parseMode(configFile.mode)
        };
    }
}
