"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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.Provision = exports.ProvisioningStatus = void 0;
const axios_1 = __importDefault(require("axios"));
const FS = __importStar(require("fs"));
const utils_1 = require("./utils");
var ProvisioningStatus;
(function (ProvisioningStatus) {
    ProvisioningStatus["None"] = "none";
    ProvisioningStatus["Requested"] = "requested";
    ProvisioningStatus["Done"] = "done";
    ProvisioningStatus["Failed"] = "failed";
})(ProvisioningStatus = exports.ProvisioningStatus || (exports.ProvisioningStatus = {}));
;
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class Provision {
    static GetProvisionCode() {
        return __awaiter(this, void 0, void 0, function* () {
            if (Provision.CodeBuffer != null) {
                return Provision.CodeBuffer;
            }
            const response = yield axios_1.default.get(`https://${Provision.HOST}/api/provision-code`);
            const code = response.data.provisionCode;
            const uuid = response.data.provisionUuid;
            console.log(`New code: ${code}`);
            console.log(`UUID: ${uuid}`);
            if (!utils_1.Utils.suppressOutputSet) {
                // update the motd
                yield utils_1.Utils.execHostCommand(
                // eslint-disable-next-line max-len
                `echo -e '\nProvision Code: ${code}\nUse this code to pair this device on https://pair.torizon.io\n' > /etc/motd`);
                // also make sure to broadcast the code
                yield utils_1.Utils.execHostCommand(
                // eslint-disable-next-line max-len
                `echo -e '\nProvision Code: ${code}\nUse this code to pair this device on https://pair.torizon.io\n' | wall`);
            }
            Provision.CodeBuffer = {
                code: code,
                uuid: uuid
            };
            return {
                code: code,
                uuid: uuid
            };
        });
    }
    static CheckCodeClaimed(uuid) {
        return __awaiter(this, void 0, void 0, function* () {
            const response = yield axios_1.default.get(
            // eslint-disable-next-line max-len
            `https://${Provision.HOST}/api/provision-code?provisionUuid=${uuid}`);
            const access = response.data.access;
            if (access != null) {
                return access;
            }
            else {
                throw new Error("Code not claimed");
            }
        });
    }
    static GetPlatformDeviceName() {
        // read the /var/sota/import/info.json content
        const info = JSON.parse(FS.readFileSync("/var/sota/import/info.json", "utf8"));
        if (info.registeredName != null && !utils_1.Utils.suppressOutputSet) {
            void utils_1.Utils.execHostCommand(
            // eslint-disable-next-line max-len
            `echo -e '\nThis device was successfully paired on Torizon Cloud!\nThe name of this device is: ${info.registeredName}\nVisit https://app.pilot.torizon.io/#/packages to deploy samples!\n' | wall`);
        }
        return info.registeredName;
    }
}
exports.Provision = Provision;
Provision.HOST = "app.torizon.io";
;
//# sourceMappingURL=provision.js.map