"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.Jon = void 0;
const axios_1 = __importDefault(require("axios"));
const provision_1 = require("./provision");
/* static class */
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class Jon {
    static Login(username, password) {
        return __awaiter(this, void 0, void 0, function* () {
            const resp = yield axios_1.default.post(Jon._urlLogin, {
                grant_type: 'password',
                client_id: 'ota-app',
                username: username,
                password: password
            }, {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            });
            if (resp.status === 200) {
                this._token = resp.data.access_token;
                return resp.data.access_token;
            }
            else {
                throw new Error("Login failed");
            }
        });
    }
    static ProvisionToken() {
        return __awaiter(this, void 0, void 0, function* () {
            if (Jon._token === null) {
                throw new Error("Login token can not be null. Please first login.");
            }
            const resp = yield axios_1.default.get(Jon._urlTokenProvi, {
                headers: {
                    Authorization: `Bearer ${Jon._token}`
                }
            });
            if (resp.status === 200) {
                this._token = null;
                return resp.data.token;
            }
            else {
                throw new Error("Get token for provisioning failed");
            }
        });
    }
    static ConfigurePollingFrequencyTo30Secs() {
        return `mkdir -p /etc/sota/conf.d/ && ` +
            // eslint-disable-next-line max-len
            `cat <<EOF | sudo tee /etc/sota/conf.d/61-custom-polling-interval.toml\n` +
            `[uptane]\n` +
            `polling_sec = 30\n` +
            `EOF`;
    }
    static ProvisionDeviceCommand(provisionToken) {
        // eslint-disable-next-line max-len
        return `curl -fsSL https://app.torizon.io/statics/scripts/provision-device.sh | ` +
            `bash -s -- ` +
            `-u https://${provision_1.Provision.HOST}/api/accounts/devices ` +
            `-t ${provisionToken}`;
    }
}
exports.Jon = Jon;
// eslint-disable-next-line max-len
Jon._urlLogin = "https://kc.torizon.io/auth/realms/ota-users/protocol/openid-connect/token";
// eslint-disable-next-line max-len
Jon._urlTokenProvi = "https://app.torizon.io/api/accounts/token";
Jon._token = null;
//# sourceMappingURL=jon.js.map