"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_native_1 = require("react-native");
const StringUtils_1 = require("./util/StringUtils");
const CloudwiseNative = react_native_1.NativeModules.CloudwiseBridge;
const CloudwiseAction_1 = require("./CloudwiseAction");
const NullAction_1 = require("./NullAction");
const Debug_1 = require("./Debug");
var counter = 0;
function getCounter() {
    return counter++;
}
exports.Cloudwise = {
    enterAction(name, platform) {
        var key = getCounter();
        if (!StringUtils_1.StringUtils.isStringNullEmptyOrWhitespace(name)) {
            CloudwiseNative.enterAction(name, key, platform);
            return new CloudwiseAction_1.CloudwiseAction(key);
        }
        else {
            Debug_1.Debug.log("Action Name was empty or null! Action will have no effect.");
            return new NullAction_1.NullAction();
        }
    },
    reportError(errorName, errorCode, platform) {
        CloudwiseNative.reportErrorWithoutStacktrace(errorName, errorCode, platform);
    },
    reportErrorWithStacktrace(isFatal, errorName, reason, stacktrace, platform) {
        let reasonNewLineIndex = reason.indexOf("\n");
        if (reasonNewLineIndex != -1) {
            reason = reason.substring(0, reasonNewLineIndex);
        }
        if (isFatal) {
            CloudwiseNative.reportCrash(errorName, reason, stacktrace, platform);
        }
        else {
            CloudwiseNative.reportError(errorName, "-", reason, stacktrace, platform);
        }
    },
    endSession(platform) {
        CloudwiseNative.endVisit(platform);
    },
    identifyUser(user, platform) {
        CloudwiseNative.identifyUser(user, platform);
    },
    setGPSLocation(latitude, longitude, platform) {
        CloudwiseNative.setGPSLocation(latitude, longitude, platform);
    },
    isCrashReportingOptedIn(platform) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield CloudwiseNative.isCrashReportingOptedIn(platform);
        });
    },
    setCrashReportingOptedIn(crashReporting, platform) {
        CloudwiseNative.setCrashReportingOptedIn(crashReporting, platform);
    },
    getDataCollectionLevel(platform) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield CloudwiseNative.getDataCollectionLevel(platform);
        });
    },
    setDataCollectionLevel(dataCollectionLevel, platform) {
        CloudwiseNative.setDataCollectionLevel(dataCollectionLevel, platform);
    },
    flushEvents(platform) {
        CloudwiseNative.flushEvents(platform);
    }
};
