/**
 * UUID regex pattern (standard 8-4-4-4-12 format).
 */
const UUID_REGEX = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i;
/**
 * Friendly sandbox ID pattern: realm-instance or realm_instance
 * - realm: 4 alphanumeric characters
 * - separator: dash or underscore
 * - instance: 1+ alphanumeric characters
 */
const FRIENDLY_ID_REGEX = /^([a-z\d]{4})[-_]([a-z\d]+)$/i;
/**
 * Prefix used in organization ID format (e.g., f_ecom_zzpq_013).
 */
const ECOM_PREFIX = 'f_ecom_';
/**
 * Strips the f_ecom_ prefix if present.
 */
function normalizeIdentifier(value) {
    if (value.toLowerCase().startsWith(ECOM_PREFIX)) {
        return value.slice(ECOM_PREFIX.length);
    }
    return value;
}
/**
 * Error thrown when a sandbox cannot be found by its friendly identifier.
 */
export class SandboxNotFoundError extends Error {
    identifier;
    realm;
    instance;
    constructor(identifier, realm, instance) {
        const message = realm && instance
            ? `Sandbox not found: ${identifier} (realm=${realm}, instance=${instance})`
            : `Sandbox not found: ${identifier}`;
        super(message);
        this.identifier = identifier;
        this.realm = realm;
        this.instance = instance;
        this.name = 'SandboxNotFoundError';
    }
}
/**
 * Checks if a string is a valid UUID.
 *
 * @param value - The string to check
 * @returns true if the value is a valid UUID
 */
export function isUuid(value) {
    return UUID_REGEX.test(value);
}
/**
 * Checks if a string matches the friendly sandbox ID format (realm-instance or realm_instance).
 *
 * @param value - The string to check
 * @returns true if the value matches the friendly format
 */
export function isFriendlySandboxId(value) {
    return FRIENDLY_ID_REGEX.test(normalizeIdentifier(value));
}
/**
 * Parses a friendly sandbox ID into its realm and instance components.
 *
 * @param value - The friendly ID to parse (e.g., "abcd-123" or "abcd_123")
 * @returns Object with realm and instance, or null if not a valid friendly ID
 */
export function parseFriendlySandboxId(value) {
    const normalized = normalizeIdentifier(value);
    const match = normalized.match(FRIENDLY_ID_REGEX);
    if (!match) {
        return null;
    }
    return {
        realm: match[1].toLowerCase(),
        instance: match[2].toLowerCase(),
    };
}
/**
 * Resolves a sandbox identifier to a UUID.
 *
 * If the identifier is already a UUID, it is returned directly without making an API call.
 * If the identifier is a friendly format (realm-instance), it queries the ODS API to find
 * the matching sandbox and returns its UUID.
 *
 * @param client - The ODS API client
 * @param identifier - Sandbox identifier (UUID or friendly format like "abcd-123")
 * @returns The sandbox UUID
 * @throws {SandboxNotFoundError} If the sandbox cannot be found
 *
 * @example
 * ```typescript
 * // UUID is returned directly
 * const uuid = await resolveSandboxId(client, 'abc12345-1234-1234-1234-abc123456789');
 * // => 'abc12345-1234-1234-1234-abc123456789'
 *
 * // Friendly ID is looked up
 * const uuid = await resolveSandboxId(client, 'zzzv-123');
 * // => 'abc12345-1234-1234-1234-abc123456789' (actual UUID from API)
 * ```
 */
export async function resolveSandboxId(client, identifier) {
    // If already a UUID, return directly
    if (isUuid(identifier)) {
        return identifier;
    }
    // Try to parse as friendly ID
    const parsed = parseFriendlySandboxId(identifier);
    if (!parsed) {
        // Not a UUID and not a friendly ID - treat as UUID and let API return 404
        return identifier;
    }
    // Query sandboxes filtered by realm
    const { data, error } = await client.GET('/sandboxes', {
        params: {
            query: {
                filter_params: `realm=${parsed.realm}`,
            },
        },
    });
    if (error || !data?.data) {
        throw new SandboxNotFoundError(identifier, parsed.realm, parsed.instance);
    }
    // Find sandbox with matching instance
    const sandbox = data.data.find((s) => s.instance?.toLowerCase() === parsed.instance);
    if (!sandbox?.id) {
        throw new SandboxNotFoundError(identifier, parsed.realm, parsed.instance);
    }
    return sandbox.id;
}
//# sourceMappingURL=sandbox-lookup.js.map