'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var minterjsUtil = require('minterjs-util');
var minterjsTx = require('minterjs-tx');
var send = require('./send.js');
var multisend = require('./multisend.js');
var convertSell = require('./convert-sell.js');
var convertBuy = require('./convert-buy.js');
var convertSellAll = require('./convert-sell-all.js');
var createCoin = require('./create-coin.js');
var candidacyDeclare = require('./candidacy-declare.js');
var candidateEdit = require('./candidate-edit.js');
var candidateSetOn = require('./candidate-set-on.js');
var stakeDelegate = require('./stake-delegate.js');
var stakeUnbond = require('./stake-unbond.js');
var redeemCheck = require('./redeem-check.js');
var createMultisig = require('./create-multisig.js');
var voteHaltBlock = require('./vote-halt-block.js');
var recreateCoin = require('./recreate-coin.js');
var editTickerOwner = require('./edit-ticker-owner.js');
var editMultisig = require('./edit-multisig.js');
var votePrice = require('./vote-price.js');
var candidateEditPublicKey = require('./candidate-edit-public-key.js');
var poolAddLiquidity = require('./pool-add-liquidity.js');
var poolRemoveLiquidity = require('./pool-remove-liquidity.js');
var poolBuy = require('./pool-buy.js');
var poolSell = require('./pool-sell.js');
var poolSellAll = require('./pool-sell-all.js');
var candidateEditCommission = require('./candidate-edit-commission.js');
var stakeMove = require('./stake-move.js');
var tokenMint = require('./token-mint.js');
var tokenBurn = require('./token-burn.js');
var tokenCreate = require('./token-create.js');
var tokenRecreate = require('./token-recreate.js');
var voteCommission = require('./vote-commission.js');
var voteUpdate = require('./vote-update.js');
var poolCreate = require('./pool-create.js');
var limitOrderAdd = require('./limit-order-add.js');
var limitOrderRemove = require('./limit-order-remove.js');
var stakeLock = require('./stake-lock.js');
var lock = require('./lock.js');
var check = require('../check.js');

var _TX_DATA_CONSTRUCTOR;
var TX_DATA_CONSTRUCTOR = (_TX_DATA_CONSTRUCTOR = {}, _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.SEND, send), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.MULTISEND, multisend), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.SELL, convertSell), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.BUY, convertBuy), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.SELL_ALL, convertSellAll), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.CREATE_COIN, createCoin), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.DECLARE_CANDIDACY, candidacyDeclare), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.EDIT_CANDIDATE, candidateEdit), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.SET_CANDIDATE_ON, candidateSetOn), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.SET_CANDIDATE_OFF, candidateSetOn), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.DELEGATE, stakeDelegate), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.UNBOND, stakeUnbond), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.REDEEM_CHECK, redeemCheck), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.CREATE_MULTISIG, createMultisig), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.SET_HALT_BLOCK, voteHaltBlock), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.RECREATE_COIN, recreateCoin), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.EDIT_TICKER_OWNER, editTickerOwner), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.EDIT_MULTISIG, editMultisig), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.PRICE_VOTE, votePrice), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.EDIT_CANDIDATE_PUBLIC_KEY, candidateEditPublicKey), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.ADD_LIQUIDITY, poolAddLiquidity), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.REMOVE_LIQUIDITY, poolRemoveLiquidity), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.BUY_SWAP_POOL, poolBuy), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.SELL_SWAP_POOL, poolSell), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.SELL_ALL_SWAP_POOL, poolSellAll), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.EDIT_CANDIDATE_COMMISSION, candidateEditCommission), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.MOVE_STAKE, stakeMove), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.MINT_TOKEN, tokenMint), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.BURN_TOKEN, tokenBurn), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.CREATE_TOKEN, tokenCreate), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.RECREATE_TOKEN, tokenRecreate), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.VOTE_COMMISSION, voteCommission), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.VOTE_UPDATE, voteUpdate), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.CREATE_SWAP_POOL, poolCreate), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.ADD_LIMIT_ORDER, limitOrderAdd), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.REMOVE_LIMIT_ORDER, limitOrderRemove), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.LOCK_STAKE, stakeLock), _defineProperty(_TX_DATA_CONSTRUCTOR, minterjsUtil.TX_TYPE.LOCK, lock), _TX_DATA_CONSTRUCTOR);

/**
 * @param {TX_TYPE|number|string|Buffer|Uint8Array} txType
 * @return {SendTxData|MultisendTxData|SellTxData|SellAllTxData|BuyTxData|CreateCoinTxData|DeclareCandidacyTxData|EditCandidateTxData|SetCandidateOnTxData|SetCandidateOffTxData|DelegateTxData|UnbondTxData|RedeemCheckTxData|CreateMultisigTxData|SetHaltBlockTxData|RecreateCoinTxData|EditTickerOwnerTxData|EditMultisigTxData|PriceVoteTxData|EditCandidatePublicKeyTxData|AddLiquidityTxData|RemoveLiquidityTxData|BuyPoolTxData|SellPoolTxData|SellAllPoolTxData|EditCandidateCommissionTxData|MoveStakeTxData|MintTokenTxData|BurnTokenTxData|CreateTokenTxData|RecreateTokenTxData|VoteCommissionTxData|VoteUpdateTxData|CreatePoolTxData|AddLimitOrderTxData|RemoveLimitOrderTxData|LockStakeTxData|LockTxData}
 */
function getTxData(txType) {
  txType = minterjsUtil.normalizeTxType(txType);
  return TX_DATA_CONSTRUCTOR[txType];
}

/**
 * @param {Buffer|TxData|object} txData
 * @param {TX_TYPE} txType
 * @param {TxOptions} [options] - options for RedeemCheckTxData
 * @return {Buffer}
 */
function ensureBufferData(txData, txType, options) {
  // serialize, if it TxData
  if (txData && typeof txData.serialize === 'function') {
    txData = txData.serialize();
  }
  // make buffer from object
  if (txData.length === undefined) {
    var TxData = getTxData(txType);
    txData = new TxData(txData, options);
    txData = txData.serialize();
  }
  return txData;
}

/**
 * Decode rlp txData into fields
 * @param {TX_TYPE} txType
 * @param {string|Buffer|Uint8Array} txData
 * @param {boolean} [decodeCheck]
 */
function decodeTxData(txType, txData) {
  var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
    isDecodeCheck = _ref.decodeCheck;
  txType = minterjsUtil.normalizeTxType(txType);
  var txDataDecoded = getTxData(txType).fromRlp(txData);
  var fields = txDataDecoded.fields;
  if (isDecodeCheck && txType === minterjsUtil.TX_TYPE.REDEEM_CHECK) {
    fields.checkData = check.decodeCheck(fields.check);
  }
  return fields;
}

/**
 * Fill tx data params with default values
 * @param {TX_TYPE} txType
 * @param {object} txData
 * @return {object}
 */
function fillDefaultData(txType, txData) {
  var defaultBufferData = new minterjsTx.TxData({}, txType, {
    forceDefaultValues: true
  });
  var defaultData = getTxData(txType).fromBufferFields(defaultBufferData, {
    disableValidation: true
  });
  var mergedData = {};
  defaultBufferData._fields.forEach(function (key) {
    if ((txData === null || txData === void 0 ? void 0 : txData[key]) !== undefined) {
      mergedData[key] = txData[key];
    } else {
      mergedData[key] = defaultData[key];
    }
  });
  return mergedData;
}

exports.decodeTxData = decodeTxData;
exports.default = getTxData;
exports.ensureBufferData = ensureBufferData;
exports.fillDefaultData = fillDefaultData;
