"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildMailConfigFromIntegration = buildMailConfigFromIntegration;
exports.getFromAddress = getFromAddress;
exports.getReplyToAddress = getReplyToAddress;
exports.buildStorageConfigFromIntegration = buildStorageConfigFromIntegration;
exports.resolveStorageEnumProvider = resolveStorageEnumProvider;
exports.resolveFileProviderSlug = resolveFileProviderSlug;
exports.buildOAuthConfigFromIntegration = buildOAuthConfigFromIntegration;
exports.buildAiConfigFromIntegration = buildAiConfigFromIntegration;
const common_1 = require("@nestjs/common");
const provider_enum_1 = require("../file/provider/provider.enum");
function buildMailConfigFromIntegration(providerSlug, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
    const cfg = (_a = config) !== null && _a !== void 0 ? _a : {};
    switch (providerSlug.toLowerCase()) {
        case 'smtp':
            return {
                global: true,
                type: 'SMTP',
                host: String((_b = cfg.host) !== null && _b !== void 0 ? _b : ''),
                port: Number((_c = cfg.port) !== null && _c !== void 0 ? _c : 587),
                secure: Boolean(cfg.secure),
                username: String((_d = cfg.username) !== null && _d !== void 0 ? _d : ''),
                password: String((_e = cfg.password) !== null && _e !== void 0 ? _e : ''),
            };
        case 'gmail':
            return {
                global: true,
                type: 'GMAIL',
                clientId: String((_f = cfg.client_id) !== null && _f !== void 0 ? _f : ''),
                clientSecret: String((_g = cfg.client_secret) !== null && _g !== void 0 ? _g : ''),
                refreshToken: String((_h = cfg.refresh_token) !== null && _h !== void 0 ? _h : ''),
                from: String((_j = cfg.from_email) !== null && _j !== void 0 ? _j : ''),
            };
        case 'ses':
            return {
                global: true,
                type: 'SES',
                region: String((_k = cfg.region) !== null && _k !== void 0 ? _k : ''),
                accessKeyId: String((_l = cfg.access_key_id) !== null && _l !== void 0 ? _l : ''),
                secretAccessKey: String((_m = cfg.secret_access_key) !== null && _m !== void 0 ? _m : ''),
                from: String((_o = cfg.from_email) !== null && _o !== void 0 ? _o : ''),
            };
        default:
            throw new common_1.BadRequestException(`Unsupported mail provider: ${providerSlug}`);
    }
}
function getFromAddress(config) {
    var _a, _b, _c;
    const cfg = (_a = config) !== null && _a !== void 0 ? _a : {};
    const fromEmail = String((_b = cfg.from_email) !== null && _b !== void 0 ? _b : '');
    const fromName = String((_c = cfg.from_name) !== null && _c !== void 0 ? _c : '');
    return fromName ? `${fromName} <${fromEmail}>` : fromEmail;
}
function getReplyToAddress(config) {
    var _a, _b, _c;
    const cfg = (_a = config) !== null && _a !== void 0 ? _a : {};
    const replyToEmail = String((_b = cfg.reply_to_email) !== null && _b !== void 0 ? _b : '').trim();
    if (!replyToEmail)
        return undefined;
    const replyToName = String((_c = cfg.reply_to_name) !== null && _c !== void 0 ? _c : '').trim();
    return replyToName ? `${replyToName} <${replyToEmail}>` : replyToEmail;
}
// ─── Storage utilities ────────────────────────────────────────────────────────
/**
 * Maps an integration_profile config JSON to the flat settings dict that the
 * existing provider classes (S3Provider, AzureProvider, etc.) expect.
 *
 * integration_provider slugs → EnumProvider:
 *   local, s3, gcs       → same
 *   azure-blob           → abs
 *   s3-compatible        → spaces
 */
function buildStorageConfigFromIntegration(providerSlug, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
    const cfg = (_a = config) !== null && _a !== void 0 ? _a : {};
    switch (providerSlug.toLowerCase()) {
        case 'local':
            return {
                'storage-local-path': String((_b = cfg.base_path) !== null && _b !== void 0 ? _b : 'storage'),
            };
        case 's3':
            return {
                'storage-s3-key': String((_c = cfg.access_key_id) !== null && _c !== void 0 ? _c : ''),
                'storage-s3-secret': String((_d = cfg.secret_access_key) !== null && _d !== void 0 ? _d : ''),
                'storage-s3-region': String((_e = cfg.region) !== null && _e !== void 0 ? _e : 'us-east-1'),
                'storage-s3-bucket': String((_f = cfg.bucket) !== null && _f !== void 0 ? _f : ''),
            };
        case 'gcs':
            return {
                'storage-gcs-keyfile': String((_g = cfg.key_file_json) !== null && _g !== void 0 ? _g : ''),
                'storage-gcs-bucket': String((_h = cfg.bucket) !== null && _h !== void 0 ? _h : ''),
            };
        case 'azure-blob':
            return {
                'storage-abs-account': String((_j = cfg.account) !== null && _j !== void 0 ? _j : ''),
                'storage-abs-key': String((_k = cfg.key) !== null && _k !== void 0 ? _k : ''),
                'storage-abs-container': String((_l = cfg.container) !== null && _l !== void 0 ? _l : ''),
            };
        case 's3-compatible':
            return {
                'storage-spaces-key': String((_m = cfg.access_key_id) !== null && _m !== void 0 ? _m : ''),
                'storage-spaces-secret': String((_o = cfg.secret_access_key) !== null && _o !== void 0 ? _o : ''),
                'storage-spaces-region': String((_p = cfg.region) !== null && _p !== void 0 ? _p : ''),
                'storage-spaces-bucket': String((_q = cfg.bucket) !== null && _q !== void 0 ? _q : ''),
                'storage-spaces-cdn': String((_r = cfg.endpoint) !== null && _r !== void 0 ? _r : ''),
            };
        default:
            throw new common_1.BadRequestException(`Unsupported storage provider: ${providerSlug}`);
    }
}
/** Maps integration_provider slug to EnumProvider. */
function resolveStorageEnumProvider(providerSlug) {
    switch (providerSlug.toLowerCase()) {
        case 'local': return provider_enum_1.EnumProvider.LOCAL;
        case 's3': return provider_enum_1.EnumProvider.S3;
        case 'gcs': return provider_enum_1.EnumProvider.GCS;
        case 'azure-blob': return provider_enum_1.EnumProvider.AZURE;
        case 's3-compatible': return provider_enum_1.EnumProvider.SPACES;
        default:
            throw new common_1.BadRequestException(`Unsupported storage provider: ${providerSlug}`);
    }
}
/** Maps integration_provider slug to the slug used in the file_provider table. */
function resolveFileProviderSlug(providerSlug) {
    switch (providerSlug.toLowerCase()) {
        case 'azure-blob': return 'abs';
        case 's3-compatible': return 'spaces';
        default: return providerSlug.toLowerCase();
    }
}
// ─── AI utilities ─────────────────────────────────────────────────────────────
// ─── OAuth utilities ──────────────────────────────────────────────────────────
/** Maps an integration_profile config JSON to OAuth client credentials. */
function buildOAuthConfigFromIntegration(_providerSlug, config) {
    var _a, _b, _c;
    const cfg = (_a = config) !== null && _a !== void 0 ? _a : {};
    return Object.assign({ clientId: String((_b = cfg.client_id) !== null && _b !== void 0 ? _b : ''), clientSecret: String((_c = cfg.client_secret) !== null && _c !== void 0 ? _c : '') }, (cfg.tenant_id ? { tenantId: String(cfg.tenant_id) } : {}));
}
// ─── AI utilities ─────────────────────────────────────────────────────────────
/** Maps an integration_profile config JSON to the API key and optional org needed by AI providers. */
function buildAiConfigFromIntegration(providerSlug, config) {
    var _a, _b, _c;
    const cfg = (_a = config) !== null && _a !== void 0 ? _a : {};
    switch (providerSlug.toLowerCase()) {
        case 'openai':
            return Object.assign({ apiKey: String((_b = cfg.api_key) !== null && _b !== void 0 ? _b : '') }, (cfg.organization ? { organization: String(cfg.organization) } : {}));
        case 'gemini':
        case 'claude':
            return { apiKey: String((_c = cfg.api_key) !== null && _c !== void 0 ? _c : '') };
        default:
            throw new common_1.BadRequestException(`Unsupported AI provider: ${providerSlug}`);
    }
}
//# sourceMappingURL=integration-profile.utils.js.map