"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const dopts = require("default-options");
const commonTypes_1 = require("../commonTypes");
const contractWrapperBase_1 = require("../contractWrapperBase");
const contractWrapperFactory_1 = require("../contractWrapperFactory");
class SchemeRegistrarWrapper extends contractWrapperBase_1.ContractWrapperBase {
    constructor() {
        super(...arguments);
        this.name = "SchemeRegistrar";
        this.frendlyName = "Scheme Registrar";
        this.factory = exports.SchemeRegistrarFactory;
        /**
         * Events
         */
        /* tslint:disable:max-line-length */
        this.NewSchemeProposal = this.createEventFetcherFactory("NewSchemeProposal");
        this.RemoveSchemeProposal = this.createEventFetcherFactory("RemoveSchemeProposal");
        this.ProposalExecuted = this.createEventFetcherFactory("ProposalExecuted");
        this.ProposalDeleted = this.createEventFetcherFactory("ProposalDeleted");
    }
    /* tslint:enable:max-line-length */
    /**
     * Note relating to permissions: According rules defined in the Controller,
     * this SchemeRegistrar is only capable of registering schemes that have
     * either no permissions or have the permission to register other schemes.
     * Therefore Arc's SchemeRegistrar is not capable of registering schemes
     * that have permissions greater than its own, thus excluding schemes having
     * the permission to add/remove global constraints or upgrade the controller.
     * The Controller will throw an exception when an attempt is made
     * to add or remove schemes having greater permissions than the scheme attempting the change.
     */
    proposeToAddModifyScheme(opts = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            /**
             * Note that explicitly supplying any property with a value of undefined will prevent the property
             * from taking on its default value (weird behavior of default-options).
             *
             */
            const defaults = {
                avatar: undefined,
                permissions: null,
                schemeAddress: undefined,
                schemeName: null,
                schemeParametersHash: undefined,
            };
            const options = dopts(opts, defaults, { allowUnknown: true });
            if (!options.avatar) {
                throw new Error("avatar is not defined");
            }
            if (!options.schemeAddress) {
                throw new Error("schemeAddress is not defined");
            }
            if (!options.schemeParametersHash) {
                throw new Error("schemeParametersHash is not defined");
            }
            /**
             * throws an Error if not valid, yields 0 if null or undefined
             */
            let permissions;
            if (options.schemeName) {
                /**
                 * then we are adding/removing an Arc scheme and can get and check its permissions.
                 */
                permissions = options.permissions || commonTypes_1.DefaultSchemePermissions[options.schemeName];
                if (permissions > this.getDefaultPermissions()) {
                    throw new Error("SchemeRegistrar cannot work with schemes having greater permissions than its own");
                }
            }
            else {
                permissions = options.permissions;
                if (!permissions) {
                    throw new Error("permissions is not defined; it is required for non-Arc schemes (where schemeName is undefined)");
                }
            }
            const tx = yield this.contract.proposeScheme(options.avatar, options.schemeAddress, options.schemeParametersHash, commonTypes_1.SchemePermissions.toString(permissions));
            return new contractWrapperBase_1.ArcTransactionProposalResult(tx);
        });
    }
    proposeToRemoveScheme(opts = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            const defaults = {
                /**
                 * avatar address
                 */
                avatar: undefined,
                /**
                 * scheme address
                 */
                schemeAddress: undefined,
            };
            const options = dopts(opts, defaults, { allowUnknown: true });
            if (!options.avatar) {
                throw new Error("avatar is not defined");
            }
            if (!options.schemeAddress) {
                throw new Error("schemeAddress address is not defined");
            }
            const tx = yield this.contract.proposeToRemoveScheme(options.avatar, options.schemeAddress);
            return new contractWrapperBase_1.ArcTransactionProposalResult(tx);
        });
    }
    setParameters(params) {
        const _super = name => super[name];
        return __awaiter(this, void 0, void 0, function* () {
            this.validateStandardSchemeParams(params);
            return _super("setParameters").call(this, params.voteParametersHash, params.voteRemoveParametersHash ? params.voteRemoveParametersHash : params.voteParametersHash, params.votingMachineAddress);
        });
    }
    getDefaultPermissions(overrideValue) {
        // return overrideValue || Utils.numberToPermissionsString(DefaultSchemePermissions.SchemeRegistrar);
        return (overrideValue || commonTypes_1.DefaultSchemePermissions.SchemeRegistrar);
    }
    getSchemePermissions(avatarAddress) {
        return __awaiter(this, void 0, void 0, function* () {
            return this._getSchemePermissions(avatarAddress);
        });
    }
    getSchemeParameters(avatarAddress) {
        return __awaiter(this, void 0, void 0, function* () {
            return this._getSchemeParameters(avatarAddress);
        });
    }
    getParameters(paramsHash) {
        return __awaiter(this, void 0, void 0, function* () {
            const params = yield this.getParametersArray(paramsHash);
            return {
                voteParametersHash: params[0],
                voteRemoveParametersHash: params[1],
                votingMachineAddress: params[2],
            };
        });
    }
}
exports.SchemeRegistrarWrapper = SchemeRegistrarWrapper;
exports.SchemeRegistrarFactory = new contractWrapperFactory_1.default("SchemeRegistrar", SchemeRegistrarWrapper);
//# sourceMappingURL=schemeregistrar.js.map