/**
 * Path utilities for takt configuration
 *
 * This module provides pure path utilities without UI dependencies.
 * For initialization with language selection, use initialization.ts.
 */
import { homedir } from 'node:os';
import { isAbsolute, join, relative, resolve } from 'node:path';
import { existsSync, mkdirSync, realpathSync } from 'node:fs';
import { LanguageSchema } from '../../core/models/schema-base.js';
import { getLanguageResourcesDir } from '../resources/index.js';
import { REPERTOIRE_DIR_NAME } from './constants.js';
import { getProjectConfigDir as resolveProjectConfigDir, getProjectConfigPath as resolveProjectConfigPath, } from './project/projectConfigPaths.js';
/** Get takt global config directory (~/.takt or TAKT_CONFIG_DIR) */
export function getGlobalConfigDir() {
    return process.env.TAKT_CONFIG_DIR || join(homedir(), '.takt');
}
/** Get takt global personas directory (~/.takt/personas) */
export function getGlobalPersonasDir() {
    return join(getGlobalConfigDir(), 'personas');
}
/** Get takt global workflows directory (~/.takt/workflows) */
export function getGlobalWorkflowsDir() {
    return join(getGlobalConfigDir(), 'workflows');
}
/** Get takt global schemas directory (~/.takt/schemas) */
export function getGlobalSchemasDir() {
    return join(getGlobalConfigDir(), 'schemas');
}
/** Get takt global logs directory */
export function getGlobalLogsDir() {
    return join(getGlobalConfigDir(), 'logs');
}
/** Get takt global config file path */
export function getGlobalConfigPath() {
    return join(getGlobalConfigDir(), 'config.yaml');
}
/** Get builtin workflows directory (builtins/{lang}/workflows) */
export function getBuiltinWorkflowsDir(lang) {
    return join(getLanguageResourcesDir(lang), 'workflows');
}
export function isBuiltinWorkflowPath(filePath) {
    const resolvedFilePath = resolve(filePath);
    return LanguageSchema.options.some((lang) => isPathSafe(getBuiltinWorkflowsDir(lang), resolvedFilePath));
}
/** Get builtin personas directory (builtins/{lang}/facets/personas) */
export function getBuiltinPersonasDir(lang) {
    return join(getLanguageResourcesDir(lang), 'facets', 'personas');
}
/** Get project takt config directory (.takt in project) */
export function getProjectConfigDir(projectDir) {
    return resolveProjectConfigDir(projectDir);
}
/** Get project workflows directory (.takt/workflows in project) */
export function getProjectWorkflowsDir(projectDir) {
    return join(getProjectConfigDir(projectDir), 'workflows');
}
/** Get project schemas directory (.takt/schemas in project) */
export function getProjectSchemasDir(projectDir) {
    return join(getProjectConfigDir(projectDir), 'schemas');
}
/** Get project config file path */
export function getProjectConfigPath(projectDir) {
    return resolveProjectConfigPath(projectDir);
}
/** Get project tasks directory */
export function getProjectTasksDir(projectDir) {
    return join(getProjectConfigDir(projectDir), 'tasks');
}
/** Get project completed tasks directory */
export function getProjectCompletedDir(projectDir) {
    return join(getProjectConfigDir(projectDir), 'completed');
}
/** Get project logs directory */
export function getProjectLogsDir(projectDir) {
    return join(getProjectConfigDir(projectDir), 'logs');
}
/** Ensure a directory exists, create if not */
export function ensureDir(dirPath) {
    if (!existsSync(dirPath)) {
        mkdirSync(dirPath, { recursive: true });
    }
}
/** Get project facet directory (.takt/facets/{facetType} in project) */
export function getProjectFacetDir(projectDir, facetType) {
    return join(getProjectConfigDir(projectDir), 'facets', facetType);
}
/** Get global facet directory (~/.takt/facets/{facetType}) */
export function getGlobalFacetDir(facetType) {
    return join(getGlobalConfigDir(), 'facets', facetType);
}
/** Get builtin facet directory (builtins/{lang}/facets/{facetType}) */
export function getBuiltinFacetDir(lang, facetType) {
    return join(getLanguageResourcesDir(lang), 'facets', facetType);
}
/** Get repertoire directory (~/.takt/repertoire/) */
export function getRepertoireDir() {
    return join(getGlobalConfigDir(), REPERTOIRE_DIR_NAME);
}
/** Get repertoire package directory (~/.takt/repertoire/@{owner}/{repo}/) */
export function getRepertoirePackageDir(owner, repo) {
    return join(getRepertoireDir(), `@${owner}`, repo);
}
/**
 * Get repertoire facet directory.
 *
 * Defaults to the global repertoire dir when repertoireDir is not specified.
 * Pass repertoireDir explicitly when resolving facets within a custom repertoire root
 * (e.g. the package-local resolution layer).
 */
export function getRepertoireFacetDir(owner, repo, facetType, repertoireDir) {
    const base = repertoireDir ?? getRepertoireDir();
    return join(base, `@${owner}`, repo, 'facets', facetType);
}
/** Validate path is safe (no directory traversal) */
export function isPathSafe(basePath, targetPath) {
    const resolvedBase = existsSync(basePath) ? realpathSync(basePath) : resolve(basePath);
    const resolvedTarget = existsSync(targetPath) ? realpathSync(targetPath) : resolve(targetPath);
    const rel = relative(resolvedBase, resolvedTarget);
    return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));
}
// Re-export project config functions
export { loadProjectConfig, saveProjectConfig, updateProjectConfig, } from './project/projectConfig.js';
export { isVerboseMode, } from './project/resolvedSettings.js';
// Re-export session storage functions
export { writeFileAtomic, getInputHistoryPath, MAX_INPUT_HISTORY, loadInputHistory, saveInputHistory, addToInputHistory, getPersonaSessionsPath, loadPersonaSessions, savePersonaSessions, updatePersonaSession, clearPersonaSessions, 
// Worktree sessions
getWorktreeSessionsDir, encodeWorktreePath, getWorktreeSessionPath, loadWorktreeSessions, updateWorktreeSession, } from './project/sessionStore.js';
//# sourceMappingURL=paths.js.map