/** The single site that derives a {@link ConnectionKey} from its two parts. */
export function connectionKey(profile, endpoint) {
    return `${profile}@${endpoint}`;
}
/**
 * Adopt a string that IS already a runtime key — a runtime directory name, a
 * `tasks.json` `profile` field, a fork key. Never use it to launder a bare
 * profile name into a key; use {@link connectionKey} for that.
 */
export function asConnectionKey(raw) {
    return raw;
}
/**
 * Split a {@link ConnectionKey} back into its parts. The inverse of
 * {@link connectionKey}, and the ONE rule every consumer uses to decide whether
 * a runtime key belongs to a profile.
 *
 * Tolerates the legacy shape still on disk: a pre-composite key that is just
 * the bare profile name (`comet-local`).
 *
 * A key with no `@` is read as a WHOLE profile name, never as `<name>.<fork>`,
 * because nothing constrains a profile's name (`createProfile` checks only for
 * a duplicate name and a port collision). Reading the trailing digits as a fork
 * would let `stop --profile chrome` claim the runtime dirs of a separate
 * profile named `chrome.2` and kill its browser. A fork is only ever created
 * from a composite base (`forkElectronProfile` appends to a `<p>@<e>` key), so
 * the `@`-bearing branch below is the one that must understand forks.
 */
export function parseConnectionKey(key) {
    // LAST `@`, not the first: a profile name may itself contain one (`me@work`),
    // and splitting on the first would report its profile as `me` — so
    // `status --profile me@work` would find nothing for a running browser.
    const at = key.lastIndexOf('@');
    if (at === -1)
        return { profile: key };
    const profile = key.slice(0, at);
    const rest = key.slice(at + 1);
    const dot = rest.indexOf('.');
    if (dot === -1)
        return { profile, endpoint: rest };
    const fork = Number(rest.slice(dot + 1));
    return {
        profile,
        endpoint: rest.slice(0, dot),
        fork: Number.isFinite(fork) ? fork : undefined,
    };
}
/** True when `key` is a runtime key of `profile`. The only reconciliation rule. */
export function keyBelongsToProfile(key, profile) {
    return parseConnectionKey(key).profile === profile;
}
export const TASK_ID_REGEX = /^[a-z0-9][a-z0-9-]*$/;
export function isValidTaskId(id) {
    return TASK_ID_REGEX.test(id) && id.length <= 64;
}
export function generateTaskId() {
    return crypto.randomUUID().slice(0, 8);
}
export function generateShortId() {
    return crypto.randomUUID().split('-')[0]; // 8 chars
}
const ADJECTIVES = [
    'swift', 'cosmic', 'jolly', 'quiet', 'bold', 'bright', 'calm', 'eager',
    'golden', 'happy', 'keen', 'lucky', 'noble', 'proud', 'quick', 'royal',
    'silver', 'amber', 'crimson', 'misty', 'sunny', 'gentle', 'wild', 'brave',
    'merry', 'sleek', 'wise', 'fierce', 'curious', 'humble', 'spry', 'witty',
];
const NOUNS = [
    'falcon', 'comet', 'tiger', 'nebula', 'phoenix', 'river', 'summit', 'wave',
    'aurora', 'breeze', 'crystal', 'dragon', 'ember', 'forest', 'glacier', 'harbor',
    'crab', 'otter', 'hawk', 'fox', 'wolf', 'panda', 'lynx', 'raven',
    'meadow', 'canyon', 'valley', 'orchid', 'cedar', 'thistle', 'lotus', 'briar',
];
export function generateFunName() {
    const adj = ADJECTIVES[Math.floor(Math.random() * ADJECTIVES.length)];
    const noun = NOUNS[Math.floor(Math.random() * NOUNS.length)];
    return `${adj}-${noun}`;
}
/**
 * Auto-generated task name: `<adjective>-<noun>-<noun>-<hex8>`, e.g.
 * `swift-crab-falcon-a3f92b1c`. Three English words make it memorable and
 * easy to read; 32 bits of hex give every spawned task enough entropy that
 * parallel agents never collide on the daemon side.
 */
export function generateTaskName() {
    const adj = ADJECTIVES[Math.floor(Math.random() * ADJECTIVES.length)];
    const noun1 = NOUNS[Math.floor(Math.random() * NOUNS.length)];
    let noun2 = NOUNS[Math.floor(Math.random() * NOUNS.length)];
    while (noun2 === noun1) {
        noun2 = NOUNS[Math.floor(Math.random() * NOUNS.length)];
    }
    const hex8 = crypto.randomUUID().replace(/-/g, '').slice(0, 8);
    return `${adj}-${noun1}-${noun2}-${hex8}`;
}
