"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CREDENTIAL_BEARING_DSN_SCHEMES = exports.SECRET_KEY_PATTERN = exports.CREDENTIAL_RESOLVERS = exports.DATABASE_LIFECYCLE_MANAGED_BY = exports.DATABASE_ENGINES = exports.ENVIRONMENT_CLASSES = exports.LIFECYCLE_OPERATIONS = exports.LIFECYCLE_MIGRATION_STATES = exports.LIFECYCLE_STATES = exports.LIFECYCLE_NON_TERMINAL_STATES = exports.LIFECYCLE_TERMINAL_STATES = void 0;
exports.isReadyDatabaseBinding = isReadyDatabaseBinding;
exports.computeRequirementKey = computeRequirementKey;
exports.computeBindingKey = computeBindingKey;
exports.computeLegacyBindingKeyWithoutOrganization = computeLegacyBindingKeyWithoutOrganization;
exports.computeDesiredSpecHash = computeDesiredSpecHash;
exports.isTerminalLifecycleState = isTerminalLifecycleState;
exports.redactLifecycleSecrets = redactLifecycleSecrets;
exports.isSecretKey = isSecretKey;
exports.containsCredentialMaterial = containsCredentialMaterial;
const hashing_js_1 = require("../signing/hashing.js");
exports.LIFECYCLE_TERMINAL_STATES = ['ready', 'failed', 'cancelled'];
exports.LIFECYCLE_NON_TERMINAL_STATES = ['pending', 'provisioning', 'migrating', 'retiring'];
exports.LIFECYCLE_STATES = [...exports.LIFECYCLE_NON_TERMINAL_STATES, ...exports.LIFECYCLE_TERMINAL_STATES];
exports.LIFECYCLE_MIGRATION_STATES = ['pending', 'migrated', 'failed'];
exports.LIFECYCLE_OPERATIONS = ['ensure_dev_database', 'ensure_prod_database', 'migrate_schema', 'retire_database'];
exports.ENVIRONMENT_CLASSES = ['dev', 'staging', 'prod'];
exports.DATABASE_ENGINES = ['postgres', 'snowflake', 'snowflake_postgres', 'lakebase'];
exports.DATABASE_LIFECYCLE_MANAGED_BY = 'database_lifecycle';
exports.CREDENTIAL_RESOLVERS = ['opa_local', 'aws_secrets_manager', 'gcp_secret_manager', 'vault', 'kubernetes_secret'];
function isReadyDatabaseBinding(binding) {
    return binding.lifecycleState === 'ready';
}
function computeRequirementKey(requirement) {
    return `${slugify(requirement.logicalName)}~${encodeURIComponent(requirement.logicalName)}:${requirement.engine}`;
}
function computeBindingKey(input) {
    return [input.organizationId, ...bindingKeySegments(input)].join(':');
}
function computeLegacyBindingKeyWithoutOrganization(input) {
    return bindingKeySegments(input).join(':');
}
function bindingKeySegments(input) {
    return [
        input.applicationId,
        input.environmentClass,
        `${slugify(input.environmentName)}~${encodeURIComponent(input.environmentName)}`,
        input.requirementKey
    ];
}
async function computeDesiredSpecHash(requirement) {
    return await (0, hashing_js_1.sha256Base64)(JSON.stringify(canonicalize(requirement)));
}
function isTerminalLifecycleState(state) {
    return exports.LIFECYCLE_TERMINAL_STATES.includes(state);
}
function redactLifecycleSecrets(value) {
    return redact(value);
}
function slugify(value) {
    return value
        .trim()
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, '-')
        .replace(/^-|-$/g, '');
}
function canonicalize(value) {
    if (Array.isArray(value)) {
        return value.map(canonicalize).sort((left, right) => codepointCompare(JSON.stringify(left), JSON.stringify(right)));
    }
    if (value && typeof value === 'object') {
        return Object.fromEntries(Object.entries(value)
            .filter(([, entryValue]) => entryValue !== undefined)
            .sort(([left], [right]) => codepointCompare(left, right))
            .map(([key, entryValue]) => [key, canonicalize(entryValue)]));
    }
    return value;
}
function codepointCompare(left, right) {
    if (left < right) {
        return -1;
    }
    if (left > right) {
        return 1;
    }
    return 0;
}
function redact(value, preserveCredentialRefs = false) {
    if (Array.isArray(value)) {
        return value.map((entry) => redact(entry, preserveCredentialRefs));
    }
    if (value && typeof value === 'object') {
        return Object.fromEntries(Object.entries(value).map(([key, entryValue]) => {
            if (isCredentialRefKey(key)) {
                return [key, redact(entryValue, true)];
            }
            if (!preserveCredentialRefs && exports.SECRET_KEY_PATTERN.test(key)) {
                return [key, '[REDACTED]'];
            }
            return [key, redact(entryValue, preserveCredentialRefs)];
        }));
    }
    return value;
}
// Shared secret-key tripwire: keys whose name strongly suggests a raw secret
// value. Used by `redactLifecycleSecrets` to scrub logs/error reporting and by
// the server-side `assertNoRawCredentialMaterial` guard to refuse callbacks
// that violate the typed-CredentialRef contract. NOT a security boundary --
// callers must still send `Record<string, string | number | boolean>` for
// `connectionMetadata` and `CredentialRef` for `credentialRefs`; this catches
// obvious misuse only.
exports.SECRET_KEY_PATTERN = /password|secret|token|private[_-]?key|dsn/i;
// Schemes whose URI form embeds credentials before `@`. Used as a value-side
// tripwire alongside `SECRET_KEY_PATTERN` -- callers should never send
// connection strings; the typed `connectionMetadata` shape is the contract.
exports.CREDENTIAL_BEARING_DSN_SCHEMES = [
    'postgres',
    'postgresql',
    'mysql',
    'mariadb',
    'mongodb',
    'mongodb+srv',
    'redis',
    'rediss',
    'amqp',
    'amqps',
    'kafka'
];
function isSecretKey(key) {
    return exports.SECRET_KEY_PATTERN.test(key);
}
function containsCredentialMaterial(value) {
    if (DSN_WITH_INLINE_AUTH.test(value)) {
        return true;
    }
    if (CREDENTIAL_KV_PATTERN.test(value)) {
        return true;
    }
    if (JSON_ENCODED_SECRET_PATTERN.test(value)) {
        return true;
    }
    return false;
}
const DSN_SCHEME_GROUP = exports.CREDENTIAL_BEARING_DSN_SCHEMES.join('|').replace(/\+/g, '\\+');
const DSN_WITH_INLINE_AUTH = new RegExp(`(?:${DSN_SCHEME_GROUP}):\\/\\/[^/@\\s:]+:[^/@\\s]+@`, 'i');
const CREDENTIAL_KV_PATTERN = /(?:password|pwd|token|secret|private[_-]?key|api[_-]?key)\s*=\s*[^;&\s"]+/i;
const JSON_ENCODED_SECRET_PATTERN = /["']\s*(?:password|secret|token|private[_-]?key|dsn)\s*["']\s*:/i;
function isCredentialRefKey(key) {
    return /credential[_-]?ref/i.test(key);
}
//# sourceMappingURL=index.js.map