"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveMediaUrlWithSas = exports.getCellMediaUrlForExport = exports.getEffectiveColumnVarType = exports.getCellMediaUrl = exports.getCellDisplayValue = exports.getMediaUrlFieldName = exports.isMediaDatasetType = exports.urlAlreadyHasAzureSasSignature = void 0;
const constants_1 = require("../../../constants");
const fetchBlobFilesFromAzure_1 = require("../../../util/fetchBlobFilesFromAzure");
const getFieldName = (f) => typeof f === 'object' ? String(f?.name ?? '') : String(f);
const getFieldType = (f) => typeof f === 'object' && f?.type
    ? String(f.type)
    : constants_1.DATASET_VAR_TYPE.TEXT;
const getUploadDatasetVarTypeFromConfig = (datasetConfig) => {
    const dataType = Number(datasetConfig?.dataType);
    if (!Number.isFinite(dataType)) {
        return null;
    }
    if (dataType === constants_1.DATASET_UPLOAD_DATA_TYPE.AUDIO) {
        return constants_1.DATASET_VAR_TYPE.VOICE;
    }
    if (dataType === constants_1.DATASET_UPLOAD_DATA_TYPE.IMAGE) {
        return constants_1.DATASET_VAR_TYPE.IMAGE;
    }
    if (dataType === constants_1.DATASET_UPLOAD_DATA_TYPE.VIDEO) {
        return constants_1.DATASET_VAR_TYPE.VIDEO;
    }
    if (dataType === constants_1.DATASET_UPLOAD_DATA_TYPE.CONVERSATION) {
        return constants_1.DATASET_VAR_TYPE.CONVERSATION;
    }
    if (dataType === constants_1.DATASET_UPLOAD_DATA_TYPE.TEXT) {
        return constants_1.DATASET_VAR_TYPE.TEXT;
    }
    return null;
};
/** Azure Blob SAS URLs include a `sig` query param; CP fetch-private-file is unnecessary. */
const urlAlreadyHasAzureSasSignature = (raw) => {
    const s = raw.trim();
    if (!/^https?:\/\//i.test(s)) {
        return false;
    }
    try {
        return new URL(s).searchParams.has('sig');
    }
    catch {
        return /[?&]sig=/i.test(s);
    }
};
exports.urlAlreadyHasAzureSasSignature = urlAlreadyHasAzureSasSignature;
const extractBlobUrl = (response) => {
    if (typeof response === 'string') {
        return response;
    }
    if (response && typeof response === 'object') {
        return (response.url ||
            response.blobUrl ||
            response.uri ||
            Object.values(response)[0]);
    }
    return undefined;
};
const isMediaDatasetType = (t) => {
    return (t === constants_1.DATASET_VAR_TYPE.VOICE ||
        t === constants_1.DATASET_VAR_TYPE.IMAGE ||
        t === constants_1.DATASET_VAR_TYPE.VIDEO ||
        t === constants_1.DATASET_VAR_TYPE.CONVERSATION);
};
exports.isMediaDatasetType = isMediaDatasetType;
const getMediaUrlFieldName = (type) => {
    if (type === constants_1.DATASET_VAR_TYPE.VOICE) {
        return 'voiceUrl';
    }
    if (type === constants_1.DATASET_VAR_TYPE.IMAGE) {
        return 'imageUrl';
    }
    if (type === constants_1.DATASET_VAR_TYPE.VIDEO) {
        return 'videoUrl';
    }
    if (type === constants_1.DATASET_VAR_TYPE.CONVERSATION) {
        return 'conversationUrl';
    }
    return null;
};
exports.getMediaUrlFieldName = getMediaUrlFieldName;
const getCellDisplayValue = (cell) => {
    if (cell == null) {
        return '';
    }
    if (typeof cell !== 'object') {
        return String(cell);
    }
    // Stored shape: { type: 'text'|'voice'|'image'|'video'|'conversation'|..., value, <type>Url }
    if (typeof cell.value === 'string' || typeof cell.value === 'number' || typeof cell.value === 'boolean') {
        return String(cell.value);
    }
    if (cell.value == null) {
        return '';
    }
    return String(cell.value);
};
exports.getCellDisplayValue = getCellDisplayValue;
const getCellMediaUrl = (cell) => {
    if (!cell || typeof cell !== 'object') {
        return null;
    }
    const type = String(cell.type || '');
    const urlField = (0, exports.getMediaUrlFieldName)(type);
    if (!urlField) {
        return null;
    }
    const raw = cell[urlField];
    if (typeof raw === 'string' && raw.length > 0) {
        return raw;
    }
    return null;
};
exports.getCellMediaUrl = getCellMediaUrl;
/** Column type from dataset schema (incl. upload `config.dataType`), not only `cell.type`. */
const getEffectiveColumnVarType = (header, fields, datasetType, datasetConfig) => {
    const list = Array.isArray(fields) ? fields : [];
    const fieldDef = list.find((f) => getFieldName(f) === header) ??
        header;
    if (datasetType === constants_1.DATASET_TYPE.UPLOAD) {
        const fromConfig = getUploadDatasetVarTypeFromConfig(datasetConfig);
        if (fromConfig) {
            return fromConfig;
        }
    }
    return getFieldType(fieldDef);
};
exports.getEffectiveColumnVarType = getEffectiveColumnVarType;
/**
 * Media URL for CSV/export using the column's effective type (e.g. upload-audio columns may omit `cell.type`).
 */
const getCellMediaUrlForExport = (cell, effectiveColumnType) => {
    if (!(0, exports.isMediaDatasetType)(effectiveColumnType)) {
        return null;
    }
    if (!cell || typeof cell !== 'object') {
        return null;
    }
    const urlField = (0, exports.getMediaUrlFieldName)(effectiveColumnType);
    if (!urlField) {
        return null;
    }
    const raw = cell[urlField];
    if (typeof raw === 'string' && raw.trim().length > 0) {
        return raw.trim();
    }
    if (effectiveColumnType === constants_1.DATASET_VAR_TYPE.VOICE) {
        const legacy = cell.url;
        if (typeof legacy === 'string' && legacy.trim().length > 0) {
            return legacy.trim();
        }
    }
    return null;
};
exports.getCellMediaUrlForExport = getCellMediaUrlForExport;
const resolveMediaUrlWithSas = async (params) => {
    const trimmed = params.blobUrl.trim();
    if ((0, exports.urlAlreadyHasAzureSasSignature)(trimmed)) {
        try {
            return decodeURI(trimmed);
        }
        catch {
            return trimmed;
        }
    }
    const resolved = await (0, fetchBlobFilesFromAzure_1.fetchBlobFileFromAzure)(params.blobUrl, params.token, params.apiKey, params.expiryInHours);
    const url = extractBlobUrl(resolved);
    return typeof url === 'string' && url.length > 0 ? decodeURI(url) : params.blobUrl;
};
exports.resolveMediaUrlWithSas = resolveMediaUrlWithSas;
//# sourceMappingURL=mediaValueUtils.js.map