import { ModeValue } from '../mode' ; import { Level , levels } from '../../level' ; import { ensure } from '../ensure' ; import { getVersionSizes , getSize } from './sizeByMode' ; const findVersion = ( mode , level , dataSize , minimumVersion = 1 ) => { for ( let i = minimumVersion - 1 ; i < 40 ; i ++ ) { const version = i + 1 ; const versionSizes = getVersionSizes ( mode , version ) ; /**
         * The level was not set by the user and one of the levels
         * in the current version can hold the data size
         */ if ( ! level && Math . max ( ... versionSizes ) >= dataSize ) { return version ; } /**
         * The level was set by the user and the current version and level
         * can hold the data size
         */ if ( level && versionSizes [ levels . indexOf ( level ) ] >= dataSize ) { return version ; } } throw new Error ( `The data size (${ dataSize }) cannot be encoded in a QR Code` ) ; } ; const findLevel = ( mode , version , dataSize ) => { const versionSizes = getVersionSizes ( mode , version ) ; const size = ensure ( size => size >= dataSize , Array . from ( versionSizes ) . reverse ( ) ) ; return levels [ versionSizes . indexOf ( size ) ] ; } ; export const selectErrorDetection = ( mode , minimumVersion , level , dataSize ) => { if ( minimumVersion && ( minimumVersion < 1 || minimumVersion > 40 ) ) { throw new Error ( 'Invalid QR Code version provided' ) ; } const targetVersion = findVersion ( mode , level , dataSize , minimumVersion ) ; const targetLevel = level ?? findLevel ( mode , targetVersion , dataSize ) ; const size = getSize ( mode , targetVersion , targetLevel ) ; return { version : targetVersion , level : targetLevel , size } ; } ;