"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 });
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const sinon_1 = __importDefault(require("sinon"));
const internal_1 = require("../../../../internal");
const mocks_1 = require("../../../../testUtils/mocks");
const utils_1 = require("../../../../types/utils");
const utilsInternalModule = __importStar(require("../../../../utils/internal"));
const Documents_1 = require("../Documents");
jest.mock('~/base/Procedure', require("../../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('Documents class', () => {
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(Documents_1.Documents.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: set', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const context = mocks_1.dsMockUtils.getContextInstance();
            const asset = mocks_1.entityMockUtils.getAssetInstance();
            const documents = new Documents_1.Documents(asset, context);
            const args = {
                documents: [
                    {
                        name: 'someName',
                        uri: 'someUri',
                        contentHash: 'someHash',
                    },
                ],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args: Object.assign({ ticker: asset.ticker }, args), transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield documents.set(args);
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: get', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should retrieve all documents linked to the Asset', () => __awaiter(void 0, void 0, void 0, function* () {
            const asset = mocks_1.entityMockUtils.getAssetInstance();
            mocks_1.dsMockUtils.createQueryStub('asset', 'assetDocuments');
            const requestPaginatedStub = sinon_1.default.stub(utilsInternalModule, 'requestPaginated');
            const expectedDocuments = [
                {
                    name: 'someDocument',
                    uri: 'someUri',
                    contentHash: '0x01',
                },
                {
                    name: 'otherDocument',
                    uri: 'otherUri',
                    contentHash: '0x02',
                },
            ];
            const entries = expectedDocuments.map(({ name, uri, contentHash, type, filedAt }, index) => utils_1.tuple({
                args: [
                    mocks_1.dsMockUtils.createMockTicker(asset.ticker),
                    mocks_1.dsMockUtils.createMockU32(new bignumber_js_1.default(index)),
                ],
            }, mocks_1.dsMockUtils.createMockDocument({
                uri: mocks_1.dsMockUtils.createMockDocumentUri(uri),
                name: mocks_1.dsMockUtils.createMockDocumentName(name),
                /* eslint-disable @typescript-eslint/naming-convention */
                content_hash: mocks_1.dsMockUtils.createMockDocumentHash({
                    H128: mocks_1.dsMockUtils.createMockU8aFixed(contentHash, true),
                }),
                doc_type: mocks_1.dsMockUtils.createMockOption(type ? mocks_1.dsMockUtils.createMockDocumentType(type) : null),
                filing_date: mocks_1.dsMockUtils.createMockOption(filedAt ? mocks_1.dsMockUtils.createMockMoment(new bignumber_js_1.default(filedAt.getTime())) : null),
                /* eslint-enable @typescript-eslint/naming-convention */
            })));
            requestPaginatedStub.resolves({ entries, lastKey: null });
            const context = mocks_1.dsMockUtils.getContextInstance();
            const documents = new Documents_1.Documents(asset, context);
            const result = yield documents.get();
            expect(result).toEqual({ data: expectedDocuments, next: null });
        }));
    });
});
//# sourceMappingURL=Documents.js.map