"use strict";

const fs = require('fs-extra');

const path = require('path');

const isExample = process.env.IS_EXAMPLE === 'TRUE';
const DEFAULT_FILE_NAME = 'keys.json';
const PROJECT_ROOT_DIR_PATH = path.join(__dirname, isExample ? '../../example/' : '../../../../');
const PACKAGE_ROOT_DIR_PATH = path.join(__dirname, '../../');
const KEYS_IOS_PATH = path.join('node_modules', 'react-native-keys', 'ios');
const KEYS_ANDROID_PATH = path.join('node_modules', 'react-native-keys', 'android');
const KEYS_IOS_EXAMPLE_PATH = path.join('../', 'ios');
const KEYS_ANDROID_EXAMPLE_PATH = path.join('../', 'android');
const IOS_DIR_PATH = path.join(PROJECT_ROOT_DIR_PATH, isExample ? KEYS_IOS_EXAMPLE_PATH : KEYS_IOS_PATH);
const ANDROID_DIR_PATH = path.join(PROJECT_ROOT_DIR_PATH, isExample ? KEYS_ANDROID_EXAMPLE_PATH : KEYS_ANDROID_PATH, 'src', 'main', 'java', 'com', 'rnkeys', 'c');
const ANDROID_KEYS_DIR_PATH = path.join(PACKAGE_ROOT_DIR_PATH, 'android', 'src', 'main', 'java', 'com', 'rnkeys');
const PROJECT_DIRECTORY_IOS_PATH = path.join(PROJECT_ROOT_DIR_PATH, 'ios');

module.exports.getKeys = KEYS_FILE_NAME => {
  const jniJsonFilePath = "".concat(PROJECT_ROOT_DIR_PATH).concat(KEYS_FILE_NAME);
  const keysJson = fs.readJSONSync(jniJsonFilePath);
  const secureKeys = keysJson;
  return secureKeys;
};

module.exports.makeFileInIosDir = (fileContent, fileName) => {
  try {
    const iosCppFilePath = path.join(IOS_DIR_PATH, fileName);
    fs.outputFileSync(iosCppFilePath, fileContent);
    return true;
  } catch (error) {
    return false;
  }
};

module.exports.makeFileInProjectDirectoryIos = (fileContent, fileName) => {
  try {
    const iosCppFilePath = path.join(PROJECT_DIRECTORY_IOS_PATH, fileName);
    fs.outputFileSync(iosCppFilePath, fileContent);
    return true;
  } catch (error) {
    return false;
  }
};

module.exports.makeFileInAndroidDir = (fileContent, fileName) => {
  try {
    const filePath = path.join(ANDROID_DIR_PATH, fileName);
    fs.outputFileSync(filePath, fileContent);
    return true;
  } catch (error) {
    return false;
  }
};

module.exports.getIosEnviromentFile = () => {
  try {
    let KEYS_FILE_NAME = process.env.KEYSFILE;

    if (KEYS_FILE_NAME) {
      return KEYS_FILE_NAME;
    } else if (process.env.CONFIGURATION === 'Debug') {
      const debugFile = process.env.DEBUG_KEYSFILE || DEFAULT_FILE_NAME;
      return debugFile;
    } else if (process.env.CONFIGURATION === 'Release') {
      const debugFile = process.env.RELEASE_KEYSFILE || DEFAULT_FILE_NAME;
      return debugFile;
    }

    return DEFAULT_FILE_NAME;
  } catch (error) {
    return DEFAULT_FILE_NAME;
  }
};

module.exports.getAndroidEnviromentFile = () => {
  try {
    let KEYS_FILE_NAME = process.env.KEYSFILE;

    if (KEYS_FILE_NAME) {
      return KEYS_FILE_NAME;
    }

    return DEFAULT_FILE_NAME;
  } catch (error) {
    return DEFAULT_FILE_NAME;
  }
};

module.exports.makeFileInAndroidForBridgeJniDir = (fileContent, fileName) => {
  try {
    const filePath = path.join(ANDROID_KEYS_DIR_PATH, fileName);
    fs.outputFileSync(filePath, fileContent);
    return true;
  } catch (error) {
    return false;
  }
};

module.exports.makeEncryptionFile = key => {
  return "\n  #include<iostream>\n  #include<string.h>\n  using namespace std;\n  #define uchar unsigned char\n  #define uint unsigned int\n\n  #define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - (c)) ++b; a += c;\n  #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))\n  #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))\n\n  #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))\n  #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))\n  #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))\n  #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))\n  #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))\n  #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))\n\n  typedef struct {\n    uchar data[64];\n    uint datalen;\n    uint bitlen[2];\n    uint state[8];\n  } SHA256_CTX;\n\n  uint k[64] = {\n    0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,\n    0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,\n    0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,\n    0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,\n    0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,\n    0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,\n    0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,\n    0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2\n  };\n\n  void SHA256Transform(SHA256_CTX *ctx, uchar data[])\n  {\n    uint a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];\n\n    for (i = 0, j = 0; i < 16; ++i, j += 4)\n      m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);\n    for (; i < 64; ++i)\n      m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];\n\n    a = ctx->state[0];\n    b = ctx->state[1];\n    c = ctx->state[2];\n    d = ctx->state[3];\n    e = ctx->state[4];\n    f = ctx->state[5];\n    g = ctx->state[6];\n    h = ctx->state[7];\n\n    for (i = 0; i < 64; ++i) {\n      t1 = h + EP1(e) + CH(e, f, g) + k[i] + m[i];\n      t2 = EP0(a) + MAJ(a, b, c);\n      h = g;\n      g = f;\n      f = e;\n      e = d + t1;\n      d = c;\n      c = b;\n      b = a;\n      a = t1 + t2;\n    }\n\n    ctx->state[0] += a;\n    ctx->state[1] += b;\n    ctx->state[2] += c;\n    ctx->state[3] += d;\n    ctx->state[4] += e;\n    ctx->state[5] += f;\n    ctx->state[6] += g;\n    ctx->state[7] += h;\n  }\n\n  void SHA256Init(SHA256_CTX *ctx)\n  {\n    ctx->datalen = 0;\n    ctx->bitlen[0] = 0;\n    ctx->bitlen[1] = 0;\n    ctx->state[0] = 0x6a09e667;\n    ctx->state[1] = 0xbb67ae85;\n    ctx->state[2] = 0x3c6ef372;\n    ctx->state[3] = 0xa54ff53a;\n    ctx->state[4] = 0x510e527f;\n    ctx->state[5] = 0x9b05688c;\n    ctx->state[6] = 0x1f83d9ab;\n    ctx->state[7] = 0x5be0cd19;\n  }\n\n  void SHA256Update(SHA256_CTX *ctx, uchar data[], uint len)\n  {\n    for (uint i = 0; i < len; ++i) {\n      ctx->data[ctx->datalen] = data[i];\n      ctx->datalen++;\n      if (ctx->datalen == 64) {\n        SHA256Transform(ctx, ctx->data);\n        DBL_INT_ADD(ctx->bitlen[0], ctx->bitlen[1], 512);\n        ctx->datalen = 0;\n      }\n    }\n  }\n\n  void SHA256Final(SHA256_CTX *ctx, uchar hash[])\n  {\n    uint i = ctx->datalen;\n\n    if (ctx->datalen < 56) {\n      ctx->data[i++] = 0x80;\n\n      while (i < 56)\n        ctx->data[i++] = 0x00;\n    }\n    else {\n      ctx->data[i++] = 0x80;\n\n      while (i < 64)\n        ctx->data[i++] = 0x00;\n\n      SHA256Transform(ctx, ctx->data);\n      memset(ctx->data, 0, 56);\n    }\n\n    DBL_INT_ADD(ctx->bitlen[0], ctx->bitlen[1], ctx->datalen * 8);\n    ctx->data[63] = ctx->bitlen[0];\n    ctx->data[62] = ctx->bitlen[0] >> 8;\n    ctx->data[61] = ctx->bitlen[0] >> 16;\n    ctx->data[60] = ctx->bitlen[0] >> 24;\n    ctx->data[59] = ctx->bitlen[1];\n    ctx->data[58] = ctx->bitlen[1] >> 8;\n    ctx->data[57] = ctx->bitlen[1] >> 16;\n    ctx->data[56] = ctx->bitlen[1] >> 24;\n    SHA256Transform(ctx, ctx->data);\n\n    for (i = 0; i < 4; ++i) {\n      hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;\n      hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;\n      hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;\n      hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;\n      hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;\n      hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;\n      hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;\n      hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;\n    }\n  }\n\n  string SHA256(char* data) {\n    int strLen = strlen(data);\n    SHA256_CTX ctx;\n    unsigned char hash[32];\n    string hashStr = \"\";\n\n    SHA256Init(&ctx);\n    SHA256Update(&ctx, (unsigned char*)data, strLen);\n    SHA256Final(&ctx, hash);\n\n    char s[3];\n    for (int i = 0; i < 32; i++) {\n      sprintf(s, \"%02x\", hash[i]);\n      hashStr += s;\n    }\n\n    return hashStr;\n  }\n  ";
};
//# sourceMappingURL=common.js.map