"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const commonTypes_1 = require("../commonTypes");
const contractWrapperFactory_1 = require("../contractWrapperFactory");
const iContractWrapperBase_1 = require("../iContractWrapperBase");
const proposalGeneratorBase_1 = require("../proposalGeneratorBase");
const web3EventService_1 = require("../web3EventService");
class VoteInOrganizationSchemeWrapper extends proposalGeneratorBase_1.ProposalGeneratorBase {
    constructor() {
        super(...arguments);
        this.name = "VoteInOrganizationScheme";
        this.friendlyName = "Vote In Organization Scheme";
        this.factory = exports.VoteInOrganizationSchemeFactory;
    }
    /**
     * Submit a proposal to vote on a proposal in another DAO.
     * @param options
     */
    proposeVoteInOrganization(options = {}) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!options.avatar) {
                throw new Error("avatar is not defined");
            }
            if (!options.originalVotingMachineAddress) {
                throw new Error("originalVotingMachineAddress is not defined");
            }
            if (!options.originalProposalId) {
                throw new Error("originalProposalId is not defined");
            }
            this.logContractFunctionCall("VoteInOrganizationScheme.proposeVote", options);
            const txResult = yield this.wrapTransactionInvocation("VoteInOrganizationScheme.proposeVote", options, this.contract.proposeVote, [options.avatar,
                options.originalVotingMachineAddress,
                options.originalProposalId]);
            return new iContractWrapperBase_1.ArcTransactionProposalResult(txResult.tx, this.contract, yield this.getVotingMachine(options.avatar));
        });
    }
    /**
     * EntityFetcherFactory for votable VoteInOrganizationProposal.
     * @param avatarAddress
     */
    getVotableProposals(avatarAddress) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this.proposalService.getProposalEvents({
                baseArgFilter: { _avatar: avatarAddress },
                proposalsEventFetcher: this.NewVoteProposal,
                transformEventCallback: (event) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                    return this.getVotableProposal(event.args._avatar, event.args._proposalId);
                }),
                votableOnly: true,
                votingMachine: yield this.getVotingMachine(avatarAddress),
            });
        });
    }
    /**
     * EntityFetcherFactory for executed proposals.
     * @param avatarAddress
     */
    getExecutedProposals(avatarAddress) {
        return this.proposalService.getProposalEvents({
            baseArgFilter: { _avatar: avatarAddress },
            proposalsEventFetcher: this.ProposalExecuted,
            transformEventCallback: (event) => {
                return Promise.resolve({
                    avatarAddress: event.args._avatar,
                    proposalId: event.args._proposalId,
                    winningVote: event.args._param,
                });
            },
        });
    }
    getVotableProposal(avatarAddress, proposalId) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const proposalParams = yield this.contract.organizationsProposals(avatarAddress, proposalId);
            return this.convertProposalPropsArrayToObject(proposalParams, proposalId);
        });
    }
    getParametersHash(params) {
        return this._getParametersHash(params.voteParametersHash, params.votingMachineAddress);
    }
    setParameters(params) {
        this.validateStandardSchemeParams(params);
        return super._setParameters("VoteInOrganizationScheme.setParameters", params.txEventContext, params.voteParametersHash, params.votingMachineAddress);
    }
    getDefaultPermissions() {
        return commonTypes_1.DefaultSchemePermissions.VoteInOrganizationScheme;
    }
    getSchemePermissions(avatarAddress) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this._getSchemePermissions(avatarAddress);
        });
    }
    getSchemeParameters(avatarAddress) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this._getSchemeParameters(avatarAddress);
        });
    }
    getParameters(paramsHash) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const params = yield this.getParametersArray(paramsHash);
            return {
                voteParametersHash: params[1],
                votingMachineAddress: params[0],
            };
        });
    }
    hydrated() {
        /* tslint:disable:max-line-length */
        this.NewVoteProposal = this.createEventFetcherFactory(this.contract.NewVoteProposal);
        this.ProposalExecuted = this.createEventFetcherFactory(this.contract.ProposalExecuted);
        this.ProposalDeleted = this.createEventFetcherFactory(this.contract.ProposalDeleted);
        /* tslint:enable:max-line-length */
    }
    convertProposalPropsArrayToObject(propsArray, proposalId) {
        return {
            originalNumOfChoices: propsArray[2].toNumber(),
            originalProposalId: propsArray[1],
            originalVotingMachineAddress: propsArray[0],
            proposalId,
        };
    }
}
exports.VoteInOrganizationSchemeWrapper = VoteInOrganizationSchemeWrapper;
exports.VoteInOrganizationSchemeFactory = new contractWrapperFactory_1.ContractWrapperFactory("VoteInOrganizationScheme", VoteInOrganizationSchemeWrapper, new web3EventService_1.Web3EventService());
//# sourceMappingURL=voteInOrganizationScheme.js.map