"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _utils() {
  const data = _interopRequireDefault(require("@ddn/utils"));

  _utils = function _utils() {
    return data;
  };

  return data;
}

var _crypto = _interopRequireDefault(require("../utils/crypto"));

var _slots = _interopRequireDefault(require("../time/slots"));

var _config = require("../config");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

const bignum = _utils().default.bignum;
/**
 * Create org transaction
 * @param {Org} org object
 * @param {*} secret
 * @param {*} second_secret
 */


function isOrgId(dao_id) {
  if (typeof dao_id !== 'string') {
    return false;
  }

  if (/^[0-9a-z_]{1,20}$/g.test(dao_id)) {
    if (dao_id.charAt(0) === '_' || dao_id.charAt(dao_id.length - 1) === '_') {
      return false; // not start or end with _
    } else {
      return true;
    }
  } else {
    return false;
  }
}

function createOrg(_x, _x2, _x3) {
  return _createOrg.apply(this, arguments);
}

function _createOrg() {
  _createOrg = _asyncToGenerator(function* (org, secret, second_secret) {
    const keys = _crypto.default.getKeys(secret);

    const sender = _crypto.default.generateAddress(keys.publicKey);

    if (!org.address) {
      org.address = sender;
    }

    if (typeof org !== 'object') {
      throw new Error('The first argument should be a object!');
    }

    org.org_id = org.org_id.toLowerCase();

    if (!isOrgId(org.org_id) || !org.org_id || org.org_id.length === 0) {
      throw new Error('Invalid org_id format');
    }

    const olen = org.org_id.length;
    let feeBase = 1;

    if (olen > 10) {
      feeBase = 10;
    } else if (olen === 10) {
      feeBase = 50;
    } else if (olen === 9) {
      feeBase = 100;
    } else if (olen === 8) {
      feeBase = 200;
    } else if (olen === 7) {
      feeBase = 400;
    } else if (olen === 6) {
      feeBase = 800;
    } else if (olen === 5) {
      feeBase = 1600;
    } else {
      // length <= 4
      feeBase = 999999; // not allow
    }

    if (org.state === 1) {
      feeBase = parseInt(feeBase / 10);
    }

    const transaction = {
      type: _utils().default.assetTypes.DAO_ORG,
      nethash: _config.config.nethash,
      amount: '0',
      fee: bignum.multiply(feeBase, 100000000).toString(),
      // bignum update feeBase * 100000000,
      recipientId: null,
      senderPublicKey: keys.publicKey,
      timestamp: _slots.default.getTime() - _config.config.clientDriftSeconds,
      asset: {
        org
      }
    };
    transaction.signature = yield _crypto.default.sign(transaction, keys);

    if (second_secret) {
      const secondKeys = _crypto.default.getKeys(second_secret);

      transaction.sign_signature = yield _crypto.default.secondSign(transaction, secondKeys);
    }

    transaction.id = yield _crypto.default.getId(transaction);
    return transaction;
  });
  return _createOrg.apply(this, arguments);
}

function createTransfer(_x4, _x5, _x6, _x7) {
  return _createTransfer.apply(this, arguments);
}

function _createTransfer() {
  _createTransfer = _asyncToGenerator(function* (address, amount, secret, second_secret) {
    const keys = _crypto.default.getKeys(secret);

    const fee = _utils().default.bignum.multiply(_config.constants.net.fees.dao_exchange, _config.constants.fixedPoint);

    const transaction = {
      type: _utils().default.assetTypes.TRANSFER,
      nethash: _config.config.nethash,
      amount,
      // fixme 1000000000 ????
      fee: `${fee}`,
      recipientId: address,
      senderPublicKey: keys.publicKey,
      timestamp: _slots.default.getTime() - _config.config.clientDriftSeconds
    };
    transaction.signature = yield _crypto.default.sign(transaction, keys);

    if (second_secret) {
      const secondKeys = _crypto.default.getKeys(second_secret);

      transaction.sign_signature = yield _crypto.default.secondSign(transaction, secondKeys);
    }

    transaction.id = yield _crypto.default.getId(transaction);
    return transaction;
  });
  return _createTransfer.apply(this, arguments);
}

function createConfirmation(_x8, _x9, _x10, _x11) {
  return _createConfirmation.apply(this, arguments);
}
/**
 * create contribution transaction
 * @param {*} contribution
 * @param {*} secret
 * @param {*} second_secret
 */


function _createConfirmation() {
  _createConfirmation = _asyncToGenerator(function* (trsAmount, confirmation, secret, second_secret) {
    const keys = _crypto.default.getKeys(secret);

    if (typeof confirmation !== 'object') {
      throw new Error('The first argument should be a object!');
    }

    if (!confirmation.sender_address || confirmation.sender_address.length === 0) {
      throw new Error('Invalid sender_address format');
    }

    if (!confirmation.received_address || confirmation.received_address.length === 0) {
      throw new Error('Invalid received_address format');
    }

    if (!confirmation.contribution_trs_id || confirmation.contribution_trs_id.length === 0) {
      throw new Error('Invalid contribution_trs_id format');
    }

    if (!confirmation.url || confirmation.url.length === 0) {
      throw new Error('Invalid url format');
    }

    if (confirmation.state !== 0 && confirmation.state !== 1) {
      throw new Error('Invalid state format');
    }

    let fee = _utils().default.bignum.multiply(_config.constants.net.fees.dao_confirmation, _config.constants.fixedPoint);

    if (confirmation.state === 0) {
      fee = '0';
    }

    let amount = '0';
    let recipientId = '';

    if (confirmation.state === 1) {
      amount = trsAmount;
      recipientId = confirmation.received_address;
    }

    const transaction = {
      type: _utils().default.assetTypes.DAO_CONFIRMATION,
      nethash: _config.config.nethash,
      amount: `${amount}`,
      fee: `${fee}`,
      recipientId,
      senderPublicKey: keys.publicKey,
      timestamp: _slots.default.getTime() - _config.config.clientDriftSeconds,
      asset: {
        daoConfirmation: confirmation
      }
    };
    transaction.signature = yield _crypto.default.sign(transaction, keys);

    if (second_secret) {
      const secondKeys = _crypto.default.getKeys(second_secret);

      transaction.sign_signature = yield _crypto.default.secondSign(transaction, secondKeys);
    }

    transaction.id = _crypto.default.getId(transaction);
    return transaction;
  });
  return _createConfirmation.apply(this, arguments);
}

function createContribution(_x12, _x13, _x14) {
  return _createContribution.apply(this, arguments);
}

function _createContribution() {
  _createContribution = _asyncToGenerator(function* (contribution, secret, second_secret) {
    const keys = _crypto.default.getKeys(secret);

    if (typeof contribution !== 'object') {
      throw new Error('The first argument should be a object!');
    }

    if (!contribution.title || contribution.title.length === 0) {
      throw new Error('Invalid title format');
    }

    if (!contribution.sender_address || contribution.sender_address.length === 0) {
      throw new Error('Invalid sender_address format');
    }

    if (!contribution.received_address || contribution.received_address.length === 0) {
      throw new Error('Invalid received_address format');
    }

    if (!contribution.url || contribution.url.length === 0) {
      throw new Error('Invalid url format');
    }

    const fee = _utils().default.bignum.multiply(_config.constants.net.fees.dao_contribution, _config.constants.fixedPoint); // contribution.sender_address = contribution.sender_address
    // contribution.received_address = contribution.received_address


    const transaction = {
      type: _utils().default.assetTypes.DAO_CONTRIBUTION,
      nethash: _config.config.nethash,
      amount: '0',
      fee: `${fee}`,
      recipientId: null,
      senderPublicKey: keys.publicKey,
      // senderPublicKey: keys.publicKey,
      timestamp: _slots.default.getTime() - _config.config.clientDriftSeconds,
      asset: {
        daoContribution: contribution
      }
    };
    transaction.signature = yield _crypto.default.sign(transaction, keys);

    if (second_secret) {
      const secondKeys = _crypto.default.getKeys(second_secret);

      transaction.sign_signature = yield _crypto.default.secondSign(transaction, secondKeys);
    }

    transaction.id = _crypto.default.getId(transaction);
    return transaction;
  });
  return _createContribution.apply(this, arguments);
}

var _default = {
  createOrg,
  createConfirmation,
  createTransfer,
  createContribution
};
exports.default = _default;