"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.linkCaDocs = exports.getAuthorization = exports.prepareLinkCaDocs = void 0;
const lodash_1 = require("lodash");
const types_1 = require("../../polkadot/types");
const internal_1 = require("../../internal");
const types_2 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareLinkCaDocs(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context: { polymeshApi: { tx: { corporateAction }, query: { asset: { assetDocuments }, }, }, }, context, } = this;
        const { id: caId, ticker, documents } = args;
        const rawAssetDocuments = yield assetDocuments.entries(conversion_1.stringToTicker(ticker, context));
        const docIdsToLink = [];
        const documentsCopy = [...documents]; // avoid mutation
        rawAssetDocuments.forEach(([key, doc]) => {
            const [, id] = key.args;
            const removedList = lodash_1.remove(documentsCopy, document => lodash_1.isEqual(document, conversion_1.documentToTokenDocument(doc)));
            if (removedList.length) {
                docIdsToLink.push(id);
            }
        });
        if (documentsCopy.length) {
            throw new internal_1.PolymeshError({
                code: types_2.ErrorCode.UnmetPrerequisite,
                message: 'Some of the provided documents are not associated with the Security Token',
                data: {
                    documents: documentsCopy,
                },
            });
        }
        const rawCaId = conversion_1.corporateActionIdentifierToCaId({ ticker, localId: caId }, context);
        this.addTransaction(corporateAction.linkCaDoc, {}, rawCaId, docIdsToLink);
    });
}
exports.prepareLinkCaDocs = prepareLinkCaDocs;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return {
        permissions: {
            tokens: [new internal_1.SecurityToken({ ticker }, this.context)],
            transactions: [types_1.TxTags.corporateAction.LinkCaDoc],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const linkCaDocs = () => new internal_1.Procedure(prepareLinkCaDocs, getAuthorization);
exports.linkCaDocs = linkCaDocs;
//# sourceMappingURL=linkCaDocs.js.map