"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ingestMediaUrl = void 0;
const logger_1 = __importDefault(require("../../../logger"));
const datasetMediaUrlResolver_1 = require("../../services/datasetRows/datasetMediaUrlResolver");
const ingestMediaUrl = async (req, res) => {
    try {
        const token = (req.headers['x-access-token'] || req.headers.authorization);
        const { url, orgId, projectId, datasetId } = (req.body ?? {});
        if (!url || !orgId || !projectId || !datasetId) {
            res.status(400).json({ error: 'url, orgId, projectId, datasetId are required' });
            return;
        }
        if (!/^https?:\/\//i.test(url)) {
            res.status(400).json({ error: 'url must be http(s)' });
            return;
        }
        const trimmed = url.trim();
        if ((0, datasetMediaUrlResolver_1.isTrusysAzureBlobStorageUrl)(trimmed)) {
            res.json({ blobUrl: trimmed });
            return;
        }
        const blobUrl = await (0, datasetMediaUrlResolver_1.ingestRemoteDatasetMediaUrl)({
            url: trimmed,
            orgId,
            projectId,
            datasetId,
            token,
            generateSasToken: false,
        });
        res.json({ blobUrl });
    }
    catch (error) {
        logger_1.default.error(`Error ingesting media url: ${error}`);
        const status = typeof error?.status === 'number' ? error.status : 500;
        res.status(status).json({ error: error?.message || 'Failed to ingest media url' });
    }
};
exports.ingestMediaUrl = ingestMediaUrl;
//# sourceMappingURL=ingest-media-url.js.map