"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.setCodecStorage = exports.setNumericStorage = exports.getStorageMapItemKey = exports.blake2_128Concat = exports.getStorageKey = void 0;
const util_crypto_1 = require("@polkadot/util-crypto");
const util_1 = require("@polkadot/util");
const encoding_1 = require("./encoding");
const parachain_1 = require("../parachain");
function getStorageKey(moduleName, storageItemName) {
    return util_crypto_1.xxhashAsHex(moduleName, 128) + encoding_1.stripHexPrefix(util_crypto_1.xxhashAsHex(storageItemName, 128));
}
exports.getStorageKey = getStorageKey;
function blake2_128Concat(data) {
    return util_crypto_1.blake2AsHex(data, 128) + encoding_1.stripHexPrefix(data);
}
exports.blake2_128Concat = blake2_128Concat;
function getStorageMapItemKey(moduleName, storageItemName, mapItemKey, nestedMapItemKey) {
    let storageKey = getStorageKey(moduleName, storageItemName) + encoding_1.stripHexPrefix(blake2_128Concat(mapItemKey));
    if (nestedMapItemKey) {
        storageKey += encoding_1.stripHexPrefix(blake2_128Concat(nestedMapItemKey));
    }
    return storageKey;
}
exports.getStorageMapItemKey = getStorageMapItemKey;
function setNumericStorage(api, moduleName, storageItemName, value, account, bits = 32, isLittleEndian = true) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = util_1.bnToHex(value, bits, isLittleEndian);
        yield setStorage(api, moduleName, storageItemName, data, account);
    });
}
exports.setNumericStorage = setNumericStorage;
function setCodecStorage(api, moduleName, storageItemName, value, account, isLittleEndian = true) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = value.toHex(isLittleEndian);
        yield setStorage(api, moduleName, storageItemName, data, account);
    });
}
exports.setCodecStorage = setCodecStorage;
function setStorage(api, moduleName, storageItemName, data, account) {
    return __awaiter(this, void 0, void 0, function* () {
        const key = getStorageKey(moduleName, storageItemName);
        const storageKey = api.createType("StorageKey", key);
        const storageData = api.createType("StorageData", data);
        const tx = api.tx.sudo.sudo(api.tx.system.setStorage([[storageKey, storageData]]));
        yield parachain_1.DefaultTransactionAPI.sendLogged(api, account, tx, undefined, true);
    });
}
//# sourceMappingURL=storage.js.map