"use strict";
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const api_1 = require("../../api");
const util_1 = require("@polkadot/util");
const Blueprint_1 = __importDefault(require("./Blueprint"));
const util_2 = require("./util");
class CodePutCodeResult extends api_1.SubmittableResult {
    constructor(result, blueprint) {
        super(result);
        this.blueprint = blueprint;
    }
}
// NOTE Experimental, POC, bound to change
class Code extends util_2.BaseWithTx {
    constructor(api, abi, decorateMethod, wasm) {
        super(api, abi, decorateMethod);
        this.createBlueprint = (maxGas) => {
            return {
                signAndSend: this.decorateMethod((account) => this.apiContracts
                    .putCode(maxGas, util_1.compactAddLength(this.code))
                    .signAndSend(account)
                    .pipe(operators_1.map(this.createResult)))
            };
        };
        this.createResult = (result) => {
            let blueprint;
            if (result.isFinalized) {
                const record = result.findRecord('contract', 'CodeStored');
                if (record) {
                    blueprint = new Blueprint_1.default(this.api, this.abi, this.decorateMethod, record.event.data[0]);
                }
            }
            return new CodePutCodeResult(result, blueprint);
        };
        this.code = util_1.u8aToU8a(wasm);
    }
}
exports.default = Code;
