"use strict";
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.Venue = exports.addInstructionTransformer = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const bluebird_1 = __importDefault(require("bluebird"));
const internal_1 = require("../../../internal");
const types_1 = require("../../../types");
const conversion_1 = require("../../../utils/conversion");
const internal_2 = require("../../../utils/internal");
/**
 * @hidden
 */
function addInstructionTransformer([instruction]) {
    return instruction;
}
exports.addInstructionTransformer = addInstructionTransformer;
/**
 * Represents a Venue through which settlements are handled
 */
class Venue extends internal_1.Entity {
    /**
     * @hidden
     */
    constructor(identifiers, context) {
        super(identifiers, context);
        const { id } = identifiers;
        this.id = id;
        this.addInstruction = internal_2.createProcedureMethod({
            getProcedureAndArgs: args => [internal_1.addInstruction, { instructions: [args], venueId: this.id }],
            transformer: addInstructionTransformer,
        }, context);
        this.addInstructions = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.addInstruction, Object.assign(Object.assign({}, args), { venueId: this.id })] }, context);
        this.modify = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.modifyVenue, Object.assign(Object.assign({}, args), { venue: this })] }, context);
    }
    /**
     * @hidden
     * Check if a value is of type [[UniqueIdentifiers]]
     */
    static isUniqueIdentifiers(identifier) {
        const { id } = identifier;
        return id instanceof bignumber_js_1.default;
    }
    /**
     * Determine whether this Venue exists on chain
     */
    exists() {
        return __awaiter(this, void 0, void 0, function* () {
            const { context: { polymeshApi: { query: { settlement }, }, }, id, context, } = this;
            const venueInfo = yield settlement.venueInfo(conversion_1.bigNumberToU64(id, context));
            return !venueInfo.isNone;
        });
    }
    /**
     * Retrieve information specific to this Venue
     */
    details() {
        return __awaiter(this, void 0, void 0, function* () {
            const { context: { polymeshApi: { query: { settlement }, }, }, id, context, } = this;
            const venueId = conversion_1.bigNumberToU64(id, context);
            const [venueInfo, details] = yield Promise.all([
                settlement.venueInfo(venueId),
                settlement.details(venueId),
            ]);
            const { creator, venue_type: type } = venueInfo.unwrap();
            return {
                owner: new internal_1.Identity({ did: conversion_1.identityIdToString(creator) }, context),
                description: conversion_1.venueDetailsToString(details),
                type: conversion_1.meshVenueTypeToVenueType(type),
            };
        });
    }
    /**
     * Retrieve all pending and failed Instructions in this Venue
     */
    getInstructions() {
        return __awaiter(this, void 0, void 0, function* () {
            const instructions = yield this.fetchInstructions();
            const failed = [];
            const pending = [];
            const details = yield bluebird_1.default.map(instructions, instruction => instruction.details());
            details.forEach(({ status }, index) => {
                if (status === types_1.InstructionStatus.Pending) {
                    pending.push(instructions[index]);
                }
                if (status === types_1.InstructionStatus.Failed) {
                    failed.push(instructions[index]);
                }
            });
            return {
                failed,
                pending,
            };
        });
    }
    /**
     * Retrieve all pending Instructions in this Venue
     *
     * @deprecated in favor of `getInstructions`
     */
    getPendingInstructions() {
        return __awaiter(this, void 0, void 0, function* () {
            const instructions = yield this.fetchInstructions();
            return bluebird_1.default.filter(instructions, (instruction) => __awaiter(this, void 0, void 0, function* () {
                const { status } = yield instruction.details();
                return status === types_1.InstructionStatus.Pending;
            }));
        });
    }
    /**
     * Fetch instructions from the chain
     */
    fetchInstructions() {
        return __awaiter(this, void 0, void 0, function* () {
            const { context: { polymeshApi: { query: { settlement }, }, }, id, context, } = this;
            const instructionEntries = yield settlement.venueInstructions.entries(conversion_1.bigNumberToU64(id, context));
            return instructionEntries.map(([{ args: [, instructionId], },]) => new internal_1.Instruction({ id: conversion_1.u64ToBigNumber(instructionId) }, context));
        });
    }
    /**
     * Return the Venue's ID
     */
    toJson() {
        return this.id.toString();
    }
}
exports.Venue = Venue;
//# sourceMappingURL=index.js.map