"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.NumberedPortfolio = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const internal_1 = require("../../internal");
const queries_1 = require("../../middleware/queries");
const types_1 = require("../../middleware/types");
const conversion_1 = require("../../utils/conversion");
const internal_2 = require("../../utils/internal");
/**
 * Represents a numbered (non-default) Portfolio for an Identity
 */
class NumberedPortfolio extends internal_1.Portfolio {
    /**
     * @hidden
     */
    constructor(identifiers, context) {
        super(identifiers, context);
        const { id, did } = identifiers;
        this.id = id;
        this.delete = internal_2.createProcedureMethod({ getProcedureAndArgs: () => [internal_1.deletePortfolio, { did, id }], voidArgs: true }, context);
        this.modifyName = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.renamePortfolio, Object.assign(Object.assign({}, args), { did, id })] }, context);
    }
    /**
     * @hidden
     * Check if a value is of type [[UniqueIdentifiers]]
     */
    static isUniqueIdentifiers(identifier) {
        const { did, id } = identifier;
        return typeof did === 'string' && id instanceof bignumber_js_1.default;
    }
    /**
     * Return the Portfolio name
     */
    getName() {
        return __awaiter(this, void 0, void 0, function* () {
            const { owner: { did }, id, context: { polymeshApi: { query: { portfolio }, }, }, context, } = this;
            const rawPortfolioName = yield portfolio.portfolios(did, conversion_1.numberToU64(id, context));
            return conversion_1.textToString(rawPortfolioName);
        });
    }
    /**
     * Retrieve the identifier data (block number, date and event index) of the event that was emitted when this Portfolio was created
     *
     * @note uses the middleware
     * @note there is a possibility that the data is not ready by the time it is requested. In that case, `null` is returned
     */
    createdAt() {
        return __awaiter(this, void 0, void 0, function* () {
            const { owner: { did }, id, context, } = this;
            const { data: { eventByIndexedArgs: event }, } = yield context.queryMiddleware(queries_1.eventByIndexedArgs({
                moduleId: types_1.ModuleIdEnum.Portfolio,
                eventId: types_1.EventIdEnum.PortfolioCreated,
                eventArg0: did,
                eventArg1: id.toString(),
            }));
            return internal_2.optionize(conversion_1.middlewareEventToEventIdentifier)(event);
        });
    }
    /**
     * Return whether this Portfolio exists
     */
    exists() {
        return __awaiter(this, void 0, void 0, function* () {
            const { owner: { did }, id, context, context: { polymeshApi: { query: { portfolio }, }, }, } = this;
            const identityId = conversion_1.stringToIdentityId(did, context);
            const rawPortfolioNumber = conversion_1.numberToU64(id, context);
            const size = yield portfolio.portfolios.size(identityId, rawPortfolioNumber);
            return !size.isZero();
        });
    }
}
exports.NumberedPortfolio = NumberedPortfolio;
//# sourceMappingURL=NumberedPortfolio.js.map