"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DAY_SHIFT_STATE_DIR_ENVIRONMENT_VARIABLE = void 0;
exports.resolveRepositoryContextPath = resolveRepositoryContextPath;
exports.resolveDayShiftUserStateDirectory = resolveDayShiftUserStateDirectory;
const node_path_1 = __importDefault(require("node:path"));
const node_os_1 = __importDefault(require("node:os"));
const paths_1 = require("../paths");
exports.DAY_SHIFT_STATE_DIR_ENVIRONMENT_VARIABLE = "DAY_SHIFT_STATE_DIR";
function resolveRepositoryContextPath(repositoryContext, repositoryRelativePath) {
    const parsedRepositoryRelativePath = typeof repositoryRelativePath === "string"
        ? (0, paths_1.parseRepositoryRelativePath)(repositoryRelativePath)
        : repositoryRelativePath;
    return {
        repositoryRoot: repositoryContext.root,
        repositoryRelativePath: parsedRepositoryRelativePath,
        absolutePath: node_path_1.default.resolve(repositoryContext.root, parsedRepositoryRelativePath.normalizedPath)
    };
}
function resolveDayShiftUserStateDirectory(options = {}) {
    const explicitStateDirectory = normalizeOptionalInput(options.stateDirectory);
    if (explicitStateDirectory !== undefined) {
        return {
            stateDirectory: explicitStateDirectory,
            source: "explicit-option"
        };
    }
    const env = options.env ?? process.env;
    const environmentStateDirectory = normalizeOptionalInput(env[exports.DAY_SHIFT_STATE_DIR_ENVIRONMENT_VARIABLE]);
    if (environmentStateDirectory !== undefined) {
        return {
            stateDirectory: environmentStateDirectory,
            source: "environment"
        };
    }
    const platform = options.platform ?? process.platform;
    const homeDirectory = options.homeDirectory ?? node_os_1.default.homedir();
    if (platform === "darwin") {
        return {
            stateDirectory: node_path_1.default.posix.join(homeDirectory, "Library", "Application Support", "day-shift"),
            source: "platform-default"
        };
    }
    if (platform === "win32") {
        const appData = normalizeOptionalInput(env.APPDATA)
            ?? node_path_1.default.win32.join(homeDirectory, "AppData", "Roaming");
        return {
            stateDirectory: node_path_1.default.win32.join(appData, "DayShift"),
            source: "platform-default"
        };
    }
    const stateHome = normalizeOptionalInput(env.XDG_STATE_HOME)
        ?? node_path_1.default.posix.join(homeDirectory, ".local", "state");
    return {
        stateDirectory: node_path_1.default.posix.join(stateHome, "day-shift"),
        source: "platform-default"
    };
}
function normalizeOptionalInput(value) {
    if (value === undefined || value.trim() === "") {
        return undefined;
    }
    return value;
}
