{"ast":null,"code":"const Utils = require('./utils');\nconst ECCode = require('./error-correction-code');\nconst ECLevel = require('./error-correction-level');\nconst Mode = require('./mode');\nconst VersionCheck = require('./version-check');\n\n// Generator polynomial used to encode version information\nconst G18 = 1 << 12 | 1 << 11 | 1 << 10 | 1 << 9 | 1 << 8 | 1 << 5 | 1 << 2 | 1 << 0;\nconst G18_BCH = Utils.getBCHDigit(G18);\nfunction getBestVersionForDataLength(mode, length, errorCorrectionLevel) {\n  for (let currentVersion = 1; currentVersion <= 40; currentVersion++) {\n    if (length <= exports.getCapacity(currentVersion, errorCorrectionLevel, mode)) {\n      return currentVersion;\n    }\n  }\n  return undefined;\n}\nfunction getReservedBitsCount(mode, version) {\n  // Character count indicator + mode indicator bits\n  return Mode.getCharCountIndicator(mode, version) + 4;\n}\nfunction getTotalBitsFromDataArray(segments, version) {\n  let totalBits = 0;\n  segments.forEach(function (data) {\n    const reservedBits = getReservedBitsCount(data.mode, version);\n    totalBits += reservedBits + data.getBitsLength();\n  });\n  return totalBits;\n}\nfunction getBestVersionForMixedData(segments, errorCorrectionLevel) {\n  for (let currentVersion = 1; currentVersion <= 40; currentVersion++) {\n    const length = getTotalBitsFromDataArray(segments, currentVersion);\n    if (length <= exports.getCapacity(currentVersion, errorCorrectionLevel, Mode.MIXED)) {\n      return currentVersion;\n    }\n  }\n  return undefined;\n}\n\n/**\n * Returns version number from a value.\n * If value is not a valid version, returns defaultValue\n *\n * @param  {Number|String} value        QR Code version\n * @param  {Number}        defaultValue Fallback value\n * @return {Number}                     QR Code version number\n */\nexports.from = function from(value, defaultValue) {\n  if (VersionCheck.isValid(value)) {\n    return parseInt(value, 10);\n  }\n  return defaultValue;\n};\n\n/**\n * Returns how much data can be stored with the specified QR code version\n * and error correction level\n *\n * @param  {Number} version              QR Code version (1-40)\n * @param  {Number} errorCorrectionLevel Error correction level\n * @param  {Mode}   mode                 Data mode\n * @return {Number}                      Quantity of storable data\n */\nexports.getCapacity = function getCapacity(version, errorCorrectionLevel, mode) {\n  if (!VersionCheck.isValid(version)) {\n    throw new Error('Invalid QR Code version');\n  }\n\n  // Use Byte mode as default\n  if (typeof mode === 'undefined') mode = Mode.BYTE;\n\n  // Total codewords for this QR code version (Data + Error correction)\n  const totalCodewords = Utils.getSymbolTotalCodewords(version);\n\n  // Total number of error correction codewords\n  const ecTotalCodewords = ECCode.getTotalCodewordsCount(version, errorCorrectionLevel);\n\n  // Total number of data codewords\n  const dataTotalCodewordsBits = (totalCodewords - ecTotalCodewords) * 8;\n  if (mode === Mode.MIXED) return dataTotalCodewordsBits;\n  const usableBits = dataTotalCodewordsBits - getReservedBitsCount(mode, version);\n\n  // Return max number of storable codewords\n  switch (mode) {\n    case Mode.NUMERIC:\n      return Math.floor(usableBits / 10 * 3);\n    case Mode.ALPHANUMERIC:\n      return Math.floor(usableBits / 11 * 2);\n    case Mode.KANJI:\n      return Math.floor(usableBits / 13);\n    case Mode.BYTE:\n    default:\n      return Math.floor(usableBits / 8);\n  }\n};\n\n/**\n * Returns the minimum version needed to contain the amount of data\n *\n * @param  {Segment} data                    Segment of data\n * @param  {Number} [errorCorrectionLevel=H] Error correction level\n * @param  {Mode} mode                       Data mode\n * @return {Number}                          QR Code version\n */\nexports.getBestVersionForData = function getBestVersionForData(data, errorCorrectionLevel) {\n  let seg;\n  const ecl = ECLevel.from(errorCorrectionLevel, ECLevel.M);\n  if (Array.isArray(data)) {\n    if (data.length > 1) {\n      return getBestVersionForMixedData(data, ecl);\n    }\n    if (data.length === 0) {\n      return 1;\n    }\n    seg = data[0];\n  } else {\n    seg = data;\n  }\n  return getBestVersionForDataLength(seg.mode, seg.getLength(), ecl);\n};\n\n/**\n * Returns version information with relative error correction bits\n *\n * The version information is included in QR Code symbols of version 7 or larger.\n * It consists of an 18-bit sequence containing 6 data bits,\n * with 12 error correction bits calculated using the (18, 6) Golay code.\n *\n * @param  {Number} version QR Code version\n * @return {Number}         Encoded version info bits\n */\nexports.getEncodedBits = function getEncodedBits(version) {\n  if (!VersionCheck.isValid(version) || version < 7) {\n    throw new Error('Invalid QR Code version');\n  }\n  let d = version << 12;\n  while (Utils.getBCHDigit(d) - G18_BCH >= 0) {\n    d ^= G18 << Utils.getBCHDigit(d) - G18_BCH;\n  }\n  return version << 12 | d;\n};","map":null,"metadata":{},"sourceType":"script","externalDependencies":[]}