"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const loggingService_1 = require("./loggingService");
const utils_1 = require("./utils");
/**
 * Methods for querying information about an Avatar's controller.
 * Use it by:
 *
 * let controllerService = new ControllerService(avatarAddress);
 *
 */
class ControllerService {
    constructor(avatarAddress) {
        this.avatarAddress = avatarAddress;
        this.isUController = undefined;
    }
    /**
     * Returns promise of whether avatar has a universal controller
     */
    getIsUController() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield this.getController();
            return this.isUController;
        });
    }
    /**
     * Returns promise of the address of the controller
     */
    getControllerAddress() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!this.controllerAddress) {
                const avatar = yield this.getAvatar();
                if (avatar) {
                    this.controllerAddress = yield avatar.owner();
                }
            }
            return this.controllerAddress;
        });
    }
    /**
     * Returns promise of a Truffle contract wrapper for the controller.  Could be
     * either UController or Controller.  You can know which one
     * by checking the ControllerService instance property `isUController`.
     */
    getController() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!this.controller) {
                const controllerAddress = yield this.getControllerAddress();
                if (controllerAddress) {
                    /**
                     * TODO:  check for previous and future versions of UController here
                     */
                    const UControllerContract = yield utils_1.Utils.requireContract("UController");
                    const ControllerContract = yield utils_1.Utils.requireContract("Controller");
                    const uControllerAddress = (yield UControllerContract.deployed()).address;
                    this.isUController = uControllerAddress === controllerAddress;
                    this.controller = this.isUController ?
                        yield UControllerContract.at(controllerAddress) :
                        yield ControllerContract.at(controllerAddress);
                }
            }
            return this.controller;
        });
    }
    /**
     * Returns promise of the Avatar Truffle contract wrapper.
     * Returns undefined if not found.
     */
    getAvatar() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!this.avatar) {
                const Avatar = yield utils_1.Utils.requireContract("Avatar");
                return Avatar.at(this.avatarAddress)
                    .then((avatar) => avatar) // only way to get to catch
                    /* have to handle the catch or promise rejection goes unhandled */
                    .catch((ex) => {
                    loggingService_1.LoggingService.error(`ControllerService: unable to load avatar at ${this.avatarAddress}: ${ex.message}`);
                    return undefined;
                });
            }
        });
    }
}
exports.ControllerService = ControllerService;
//# sourceMappingURL=controllerService.js.map