"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FactorioAnalyticsApi = void 0;
const axios_1 = __importDefault(require("axios"));
/*
* implement and use the API here for the package itself
* IN THE FUTURE - this should have an option to take all requests and save them LOCALLY to a database (sqlite)
*   - this way, any subsequent requests for data (if the trial is done) can be done locally
* */
class FactorioAnalyticsApi {
    // no need to 'startup' or anything else here - just static paths that can access the API
    static queryStatusOnTick(execId, interval = 2000) {
        return __awaiter(this, void 0, void 0, function* () {
            if (interval && interval < 2000)
                interval = 2000;
            return yield new Promise((resolve, reject) => {
                let retryCount = 0;
                let handle = setInterval(() => __awaiter(this, void 0, void 0, function* () {
                    try {
                        let status = yield FactorioAnalyticsApi.queryStatus(execId);
                        retryCount = 0;
                        if ((status === null || status === void 0 ? void 0 : status.success) != null) {
                            clearInterval(handle);
                            resolve(status);
                        }
                    }
                    catch (e) {
                        retryCount++;
                        if (retryCount > 2) {
                            clearInterval(handle);
                            reject(e);
                        }
                    }
                }), interval);
            });
        });
    }
    static submit(params) {
        return __awaiter(this, void 0, void 0, function* () {
            // submit a new request to the API - same params and everything
            const resp = yield axios_1.default.post('https://api.factorioanalytics.com/submit', params);
            if (resp.status === 200)
                return resp.data;
            else
                throw new Error('Failed to submit request');
        });
    }
    static submitSource(bpStr) {
        return __awaiter(this, void 0, void 0, function* () {
            const params = {
                variant: 'source',
                source: bpStr
            };
            return yield FactorioAnalyticsApi.submit(params);
        });
    }
    static submitTrial(trialParams) {
        return __awaiter(this, void 0, void 0, function* () {
            const params = {
                variant: 'trial',
                trial: trialParams
            };
            return yield FactorioAnalyticsApi.submit(params);
        });
    }
    static submitQuick(bpStr, modList = []) {
        return __awaiter(this, void 0, void 0, function* () {
            const params = {
                blueprintStr: bpStr,
                modList: modList ? modList : undefined
            };
            const resp = yield axios_1.default.post('https://api.factorioanalytics.com/quickSubmit', params);
            if (resp.status === 200)
                return resp.data;
            else
                throw new Error('Failed to submit request (quick)');
        });
    }
    static submitModList(mods) {
        return __awaiter(this, void 0, void 0, function* () {
            throw new Error('Not yet implemented - while modding works, starting small and simple first');
        });
    }
    static query(variant, id) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield axios_1.default.get(`https://api.factorioanalytics.com/query/${id}/${variant}`);
            if (resp.status === 200)
                return resp.data;
            else
                throw new Error('Failed to query - ' + resp.statusText);
        });
    }
    static queryTrial(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield FactorioAnalyticsApi.query('trial', id);
            return resp;
        });
    }
    static querySource(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield FactorioAnalyticsApi.query('source', id);
            return resp;
        });
    }
    static queryStatus(execId) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield FactorioAnalyticsApi.query('status', execId);
            return resp;
        });
    }
    static queryItemData(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield FactorioAnalyticsApi.query('data_item', id);
            if (resp === null || resp === void 0 ? void 0 : resp.data) {
                return resp.data;
            }
            else
                throw new Error('Failed to query item data');
        });
    }
    static queryElectricData(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield FactorioAnalyticsApi.query('data_electric', id);
            if (resp === null || resp === void 0 ? void 0 : resp.data) {
                return resp.data;
            }
            else
                throw new Error('Failed to query electric data');
        });
    }
    static queryCircuitData(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield FactorioAnalyticsApi.query('data_circuit', id);
            if (resp === null || resp === void 0 ? void 0 : resp.data) {
                return resp.data;
            }
            else
                throw new Error('Failed to query circuit data');
        });
    }
    static queryPollutionData(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield FactorioAnalyticsApi.query('data_pollution', id);
            if (resp === null || resp === void 0 ? void 0 : resp.data) {
                return resp.data;
            }
            else
                throw new Error('Failed to query pollution data');
        });
    }
    static querySystemData(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield FactorioAnalyticsApi.query('data_system', id);
            if (resp === null || resp === void 0 ? void 0 : resp.data) {
                return resp.data;
            }
            else
                throw new Error('Failed to query system data');
        });
    }
    static queryAllData(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield FactorioAnalyticsApi.query('data_all', id);
            return resp;
        });
    }
}
exports.FactorioAnalyticsApi = FactorioAnalyticsApi;
//# sourceMappingURL=FactorioAnalyticsApi.js.map