/**
 * SupervisorActor Value Object
 *
 * Encodes actor namespaces used to attribute approval-gate / question
 * decisions:
 * - `user:<id>`        — the human user
 * - `supervisor:<id>`  — a delegated supervisor agent
 *
 * The "user always wins" invariant from spec 093 is modelled here:
 * `isUserOverrideOf(prior)` returns true exactly when `self` is a user
 * actor and `prior` is a supervisor actor (any pair of ids), expressing
 * that a subsequent user action overrides a prior supervisor action on
 * the same gate / question.
 */
export const SUPERVISOR_ACTOR_NAMESPACE_USER = 'user';
export const SUPERVISOR_ACTOR_NAMESPACE_SUPERVISOR = 'supervisor';
const VALID_NAMESPACES = [
    SUPERVISOR_ACTOR_NAMESPACE_USER,
    SUPERVISOR_ACTOR_NAMESPACE_SUPERVISOR,
];
export class InvalidSupervisorActorError extends Error {
    constructor(value) {
        super(`Invalid supervisor actor: "${value}" — expected "user:<id>" or "supervisor:<id>"`);
        this.name = 'InvalidSupervisorActorError';
        Object.setPrototypeOf(this, new.target.prototype);
    }
}
class SupervisorActorImpl {
    namespace;
    id;
    constructor(namespace, id) {
        this.namespace = namespace;
        this.id = id;
        Object.freeze(this);
    }
    get value() {
        return `${this.namespace}:${this.id}`;
    }
    toString() {
        return this.value;
    }
    equals(other) {
        return this.namespace === other.namespace && this.id === other.id;
    }
    isUserOverrideOf(prior) {
        return (this.namespace === SUPERVISOR_ACTOR_NAMESPACE_USER &&
            prior.namespace === SUPERVISOR_ACTOR_NAMESPACE_SUPERVISOR);
    }
}
function isValidNamespace(candidate) {
    return VALID_NAMESPACES.includes(candidate);
}
function buildActor(namespace, id) {
    return new SupervisorActorImpl(namespace, id);
}
/**
 * Parses an actor string of the form `<namespace>:<id>`.
 * Throws {@link InvalidSupervisorActorError} on malformed input.
 */
export function parseSupervisorActor(value) {
    if (typeof value !== 'string') {
        throw new InvalidSupervisorActorError(String(value));
    }
    const colonIndex = value.indexOf(':');
    if (colonIndex <= 0 || colonIndex === value.length - 1) {
        throw new InvalidSupervisorActorError(value);
    }
    const namespace = value.slice(0, colonIndex);
    const id = value.slice(colonIndex + 1);
    if (!isValidNamespace(namespace)) {
        throw new InvalidSupervisorActorError(value);
    }
    if (id.includes(':') || id.trim().length === 0) {
        throw new InvalidSupervisorActorError(value);
    }
    return buildActor(namespace, id);
}
export function userActor(id) {
    return parseSupervisorActor(`${SUPERVISOR_ACTOR_NAMESPACE_USER}:${id}`);
}
export function supervisorActor(id) {
    return parseSupervisorActor(`${SUPERVISOR_ACTOR_NAMESPACE_SUPERVISOR}:${id}`);
}
