"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const utils_1 = require("./utils");
/**
 * Methods for querying information about an Avatar.
 * Use it by:
 *
 * let avatarService = new AvatarService(avatarAddress);
 *
 */
class AvatarService {
    constructor(avatarAddress) {
        this.avatarAddress = avatarAddress;
        this.isUController = undefined;
    }
    /**
     * Returns promise of the Avatar TruffleContract
     */
    getAvatar() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!this.avatar) {
                const Avatar = yield utils_1.Utils.requireContract("Avatar");
                this.avatar = yield Avatar.at(this.avatarAddress);
            }
            return this.avatar;
        });
    }
    /**
     * 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();
                this.controllerAddress = yield avatar.owner();
            }
            return this.controllerAddress;
        });
    }
    /**
     * Returns promise of a TruffleContract for the controller.  Could be
     * either UController or Controller.  You can know which one
     * by checking the AvatarService instance property `isUController`.
     */
    getController() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!this.controller) {
                const controllerAddress = yield this.getControllerAddress();
                /**
                 * 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 address of the avatar's native reputation.
     */
    getNativeReputationAddress() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!this.nativeReputationAddress) {
                const avatar = yield this.getAvatar();
                this.nativeReputationAddress = yield avatar.nativeReputation();
            }
            return this.nativeReputationAddress;
        });
    }
    /**
     * Returns promise of the avatar's native reputation TruffleContract.
     */
    getNativeReputation() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!this.nativeReputation) {
                const reputationAddress = yield this.getNativeReputationAddress();
                const Reputation = yield utils_1.Utils.requireContract("Reputation");
                this.nativeReputation = yield Reputation.at(reputationAddress);
            }
            return this.nativeReputation;
        });
    }
    /**
     * Returns promise of the address of the avatar's native token.
     */
    getNativeTokenAddress() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!this.nativeTokenAddress) {
                const avatar = yield this.getAvatar();
                this.nativeTokenAddress = yield avatar.nativeToken();
            }
            return this.nativeTokenAddress;
        });
    }
    /**
     * Returns promise of the avatar's native token TruffleContract.
     * Assumes the token is a `DAOToken`.
     */
    getNativeToken() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!this.nativeToken) {
                const tokenAddress = yield this.getNativeTokenAddress();
                this.nativeToken = (yield (yield utils_1.Utils.requireContract("DAOToken")).at(tokenAddress));
            }
            return this.nativeToken;
        });
    }
}
exports.AvatarService = AvatarService;
//# sourceMappingURL=avatarService.js.map