"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const instrumentor = require("./instrumentor/cloudwise-instrumentation");
const pathsConstants_1 = require("../scripts/pathsConstants");
const config = require("../scripts/config");
const logger_1 = require("../scripts/logger");
const semver_1 = require("semver");
const nodePath = require("path");
const reactNativeVersionString = require('react-native/package.json').version;
const reactNativeMinorVersion = semver_1.minor(reactNativeVersionString);
function transform(src, filename, options) {
    if (typeof src === 'object') {
        ({ src, filename, options } = src);
    }
    let reactOptions = undefined;
    try {
        reactOptions = config.readConfig(nodePath.join(pathsConstants_1.default.getConfigFilePath()));
        src = instrumentor.instrument(src, filename, reactOptions.react, reactNativeMinorVersion);
    }
    catch (e) {
        if (e.message === config.ERROR_CONFIG_NOT_AVAILABLE) {
            console.log("cloudwise.config.js not found!");
        }
        else {
            console.log("Couldn't instrument file: " + filename);
            logger_1.default.logErrorSync(e.message);
        }
    }
    return getUpstreamTransformer(reactOptions).transform({ src, filename, options });
}
function getUpstreamTransformer(reactOptions) {
    if (reactOptions !== undefined && reactOptions.react.upstreamTransformer !== undefined) {
        return require(reactOptions.react.upstreamTransformer);
    }
    else if (reactNativeMinorVersion >= 59) {
        return require('metro-react-native-babel-transformer/src/index');
    }
    else if (reactNativeMinorVersion >= 56) {
        return require('metro/src/reactNativeTransformer');
    }
    else if (reactNativeMinorVersion >= 52) {
        return require('metro/src/transformer');
    }
    else if (reactNativeMinorVersion >= 0.48) {
        return require('metro-bundler/src/transformer');
    }
    else {
    }
}
module.exports.getUpstreamTransformer = getUpstreamTransformer;
module.exports.transform = transform;
