"use strict";
/**
 * Channel utility functions for deriving and managing channel names
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.deriveChannelFromBranch = deriveChannelFromBranch;
exports.deriveDefaultChannel = deriveDefaultChannel;
exports.isValidChannel = isValidChannel;
exports.normalizeChannel = normalizeChannel;
exports.createSessionWithGitInfo = createSessionWithGitInfo;
/**
 * Derives a channel name from a git branch name
 * @param branch - Git branch name
 * @returns Derived channel name (max 20 chars) or null if branch should be skipped
 */
function deriveChannelFromBranch(branch) {
    if (!branch || branch.trim() === '')
        return null;
    // Skip main and master branches - they should not have their own channels
    if (branch === 'main' || branch === 'master')
        return null;
    // Replace special characters with hyphens
    let channel = branch
        .toLowerCase()
        .replace(/[^a-z0-9-_]/g, '-')
        .replace(/--+/g, '-')
        .replace(/^-|-$/g, '');
    // If channel is empty after cleaning, return general
    if (!channel)
        return 'general';
    // Truncate to 20 characters
    if (channel.length > 20) {
        channel = channel.substring(0, 20);
    }
    return channel;
}
/**
 * Derives a default channel name from branch or session name
 * @param branch - Git branch name (optional)
 * @param sessionName - Session name (optional)
 * @returns Derived channel name (max 20 chars) or 'general' if no inputs
 */
function deriveDefaultChannel(branch, sessionName) {
    // First try to derive from branch
    if (branch) {
        const branchChannel = deriveChannelFromBranch(branch);
        if (branchChannel) {
            return branchChannel;
        }
    }
    // If branch derivation failed or returned null (main/master), try session name
    if (sessionName) {
        const sessionChannel = deriveChannelFromBranch(sessionName);
        if (sessionChannel) {
            return sessionChannel;
        }
    }
    // Default fallback
    return 'general';
}
/**
 * Validates a channel name
 * @param channel - Channel name to validate
 * @returns true if valid, false otherwise
 */
function isValidChannel(channel) {
    if (!channel || typeof channel !== 'string') {
        return false;
    }
    // Check length
    if (channel.length === 0 || channel.length > 20) {
        return false;
    }
    // Check format (lowercase letters, numbers, hyphens only)
    return /^[a-z0-9]+(-[a-z0-9]+)*$/.test(channel);
}
/**
 * Normalizes a channel name to ensure it's valid
 * @param channel - Channel name to normalize
 * @returns Normalized channel name
 */
function normalizeChannel(channel) {
    if (!channel || typeof channel !== 'string') {
        return 'general';
    }
    // Apply same logic as deriveDefaultChannel
    const normalized = channel
        .toLowerCase()
        .trim()
        .replace(/[^a-z0-9]+/g, '-')
        .replace(/^-+|-+$/g, '');
    if (!normalized) {
        return 'general';
    }
    // Truncate if needed
    if (normalized.length > 20) {
        return normalized.substring(0, 20).replace(/-+$/, '');
    }
    return normalized;
}
/**
 * Creates a session configuration with automatic channel derivation from git
 * @param options - Options including name and git operations
 * @returns Session configuration with derived channel
 */
async function createSessionWithGitInfo(options) {
    const { v4: uuidv4 } = await Promise.resolve().then(() => __importStar(require('uuid')));
    const branch = await options.git.getCurrentBranch();
    const channel = deriveChannelFromBranch(branch || '') ||
        deriveChannelFromBranch(options.name || '') ||
        'general';
    return {
        id: uuidv4(),
        name: options.name || `Session ${Date.now()}`,
        default_channel: channel,
    };
}
