{"version":3,"file":"currency-core.bundle.js","sources":["../src/currency-item.js","../src/currency-core.js"],"sourcesContent":["// @flow\nexport class CurrencyItem\n{\n\t#currency = '';\n\t#format = {};\n\n\tconstructor(currency: string, format: {})\n\t{\n\t\tthis.#currency = currency;\n\t\tthis.#format = format;\n\t}\n\n\tgetCurrency(): string\n\t{\n\t\treturn this.#currency;\n\t}\n\n\tgetFormat(): {}\n\t{\n\t\treturn this.#format;\n\t}\n\n\tsetFormat(format: {}): void\n\t{\n\t\tthis.#format = format;\n\t}\n}","// @flow\n\nimport { Reflection, Type, Extension } from 'main.core';\nimport { CurrencyItem } from './currency-item';\n\nexport class CurrencyCore\n{\n\tstatic currencies: CurrencyItem[] = [];\n\n\tstatic defaultFormat = {\n\t\tFORMAT_STRING: '#',\n\t\tDEC_POINT: '.',\n\t\tTHOUSANDS_SEP: ' ',\n\t\tDECIMALS: 2,\n\t\tHIDE_ZERO: 'N',\n\t};\n\n\tstatic region: string = '';\n\n\tstatic getCurrencyList(): CurrencyItem[]\n\t{\n\t\treturn this.currencies;\n\t}\n\n\tstatic setCurrencyFormat(currency: string, format, replace: boolean): void\n\t{\n\t\tif (!Type.isStringFilled(currency) || !Type.isPlainObject(format))\n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\tconst index = this.getCurrencyIndex(currency);\n\n\t\tif (index > -1 && !replace)\n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\tconst innerFormat = { ...this.defaultFormat, ...format };\n\n\t\tif (index === -1)\n\t\t{\n\t\t\tthis.currencies.push(new CurrencyItem(currency, innerFormat));\n\t\t}\n\t\telse\n\t\t{\n\t\t\tthis.currencies[index].setFormat(innerFormat);\n\t\t}\n\t}\n\n\tstatic setCurrencies(currencies: [], replace: boolean)\n\t{\n\t\tif (Type.isArray(currencies))\n\t\t{\n\t\t\tfor (let i = 0; i < currencies.length; i++)\n\t\t\t{\n\t\t\t\tif (\n\t\t\t\t\t!Type.isPlainObject(currencies[i])\n\t\t\t\t\t|| !Type.isStringFilled(currencies[i].CURRENCY)\n\t\t\t\t\t|| !Type.isPlainObject(currencies[i].FORMAT)\n\t\t\t\t)\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tthis.setCurrencyFormat(currencies[i].CURRENCY, currencies[i].FORMAT, replace);\n\t\t\t}\n\t\t}\n\t}\n\n\tstatic getCurrencyFormat(currency: string)\n\t{\n\t\tconst index = this.getCurrencyIndex(currency);\n\n\t\treturn (index > -1 ? this.getCurrencyList()[index].getFormat() : false);\n\t}\n\n\tstatic getCurrencyIndex(currency: string): number\n\t{\n\t\tconst currencyList = this.getCurrencyList();\n\n\t\tfor (let i = 0; i < currencyList.length; i++)\n\t\t{\n\t\t\tif (currencyList[i].getCurrency() === currency)\n\t\t\t{\n\t\t\t\treturn i;\n\t\t\t}\n\t\t}\n\n\t\treturn -1;\n\t}\n\n\tstatic clearCurrency(currency)\n\t{\n\t\tconst index = this.getCurrencyIndex(currency);\n\t\tif (index > -1)\n\t\t{\n\t\t\tthis.currencies = BX.util.deleteFromArray(this.currencies, index);\n\t\t}\n\t}\n\n\tstatic clean()\n\t{\n\t\tthis.currencies = [];\n\t}\n\n\tstatic initRegion(): void\n\t{\n\t\tif (this.region === '')\n\t\t{\n\t\t\tconst settings = Extension.getSettings('currency.currency-core');\n\t\t\tthis.region = settings.get('region') || '-';\n\t\t}\n\t}\n\n\tstatic checkInrFormat(currency: string): boolean\n\t{\n\t\tthis.initRegion();\n\n\t\treturn (\n\t\t\tcurrency === 'INR'\n\t\t\t&& (this.region === 'hi' || this.region === 'in')\n\t\t);\n\t}\n\n\tstatic currencyFormat(price: number, currency: string, useTemplate: boolean)\n\t{\n\t\tlet result = '';\n\n\t\tconst format = this.getCurrencyFormat(currency);\n\t\tif (Type.isObject(format))\n\t\t{\n\t\t\tprice = Number(price);\n\n\t\t\tlet currentDecimals = format.DECIMALS;\n\t\t\tconst separator = format.SEPARATOR || format.THOUSANDS_SEP;\n\n\t\t\tif (format.HIDE_ZERO === 'Y' && Type.isInteger(price))\n\t\t\t{\n\t\t\t\tcurrentDecimals = 0;\n\t\t\t}\n\n\t\t\tif (this.checkInrFormat(currency))\n\t\t\t{\n\t\t\t\tresult = this.numberFormatInr(\n\t\t\t\t\tprice,\n\t\t\t\t\tcurrentDecimals,\n\t\t\t\t\tformat.DEC_POINT,\n\t\t\t\t\tseparator,\n\t\t\t\t);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tresult = BX.util.number_format(\n\t\t\t\t\tprice,\n\t\t\t\t\tcurrentDecimals,\n\t\t\t\t\tformat.DEC_POINT,\n\t\t\t\t\tseparator,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (useTemplate)\n\t\t\t{\n\t\t\t\tresult = format.FORMAT_STRING.replace(/(^|[^&])#/, '$1' + result);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tstatic getPriceControl(control: Element, currency: string)\n\t{\n\t\tlet result = '';\n\n\t\tconst format = this.getCurrencyFormat(currency);\n\t\tif (Type.isObject(format))\n\t\t{\n\t\t\tresult = format.FORMAT_STRING.replace(/(^|[^&])#/, '$1' + control.outerHTML);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tstatic loadCurrencyFormat(currency)\n\t{\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst index = this.getCurrencyIndex(currency);\n\t\t\tif (index > -1)\n\t\t\t{\n\t\t\t\tresolve(this.getCurrencyList()[index].getFormat());\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tBX.ajax.runAction('currency.format.get', { data: { currencyId: currency } })\n\t\t\t\t\t.then((response) => {\n\t\t\t\t\t\tconst format = response.data;\n\t\t\t\t\t\tthis.setCurrencyFormat(currency, format);\n\t\t\t\t\t\tresolve(format);\n\t\t\t\t\t})\n\t\t\t\t\t.catch((response) => {\n\t\t\t\t\t\treject(response.errors);\n\t\t\t\t\t});\n\t\t\t}\n\t\t})\n\t}\n\n\tstatic numberFormatInr(value: number, decimals: number, decPoint: string, thousandsSep: string): string\n\t{\n\t\tif (Number.isNaN(decimals) || decimals < 0)\n\t\t{\n\t\t\tdecimals = 2;\n\t\t}\n\t\tdecPoint = decPoint || ',';\n\t\tthousandsSep = thousandsSep || '.';\n\n\t\tlet sign: string = '';\n\t\tvalue = (+value || 0).toFixed(decimals);\n\t\tif (value < 0)\n\t\t{\n\t\t\tsign = '-';\n\t\t\tvalue = -value;\n\t\t}\n\n\t\tlet i: string = parseInt(value, 10).toString();\n\n\t\tlet km = '';\n\t\tlet kw;\n\n\t\tif (i.length <= 3)\n\t\t{\n\t\t\tkw = i;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tconst rightTriad: string = thousandsSep + i.slice(-3);\n\t\t\tconst leftBlock: string = i.slice(0,-3);\n\t\t\tconst j = (leftBlock.length > 2 ? leftBlock.length % 2 : 0);\n\n\t\t\tkm = (j ? leftBlock.slice(0, j) + thousandsSep : '');\n\t\t\tkw = leftBlock.slice(j).replace(/(\\d{2})(?=\\d)/g, \"$1\" + thousandsSep) + rightTriad;\n\t\t}\n\t\tlet kd = (\n\t\t\tdecimals\n\t\t\t\t? decPoint + Math.abs(value - i).toFixed(decimals).replace(/-/, '0').slice(2)\n\t\t\t\t: ''\n\t\t);\n\n\t\treturn sign + km + kw + kd;\n\t}\n}\n\n/** @deprecated use import { CurrencyCore } from 'currency.core' */\nReflection.namespace('BX.Currency').Core = CurrencyCore;"],"names":["CurrencyItem","currency","format","CurrencyCore","currencies","replace","Type","isStringFilled","isPlainObject","index","getCurrencyIndex","innerFormat","defaultFormat","push","setFormat","isArray","i","length","CURRENCY","FORMAT","setCurrencyFormat","getCurrencyList","getFormat","currencyList","getCurrency","BX","util","deleteFromArray","region","settings","Extension","getSettings","get","initRegion","price","useTemplate","result","getCurrencyFormat","isObject","Number","currentDecimals","DECIMALS","separator","SEPARATOR","THOUSANDS_SEP","HIDE_ZERO","isInteger","checkInrFormat","numberFormatInr","DEC_POINT","number_format","FORMAT_STRING","control","outerHTML","Promise","resolve","reject","ajax","runAction","data","currencyId","then","response","errors","value","decimals","decPoint","thousandsSep","isNaN","sign","toFixed","parseInt","toString","km","kw","rightTriad","slice","leftBlock","j","kd","Math","abs","Reflection","namespace","Core"],"mappings":";;;;;;;;;AACA,KAAaA,YAAY;GAKxB,sBAAYC,QAAgB,EAAEC,MAAU,EACxC;KAAA;KAAA;OAAA;OAAA,OAJY;;KAAE;OAAA;OAAA,OACJ;;KAIT,sCAAI,aAAaD,QAAQ;KACzB,sCAAI,WAAWC,MAAM;;GACrB;KAAA;KAAA,8BAGD;OACC,yCAAO,IAAI;;;KACX;KAAA,4BAGD;OACC,yCAAO,IAAI;;;KACX;KAAA,0BAESA,MAAU,EACpB;OACC,sCAAI,WAAWA,MAAM;;;GACrB;CAAA;;;;ACvBF,KAGaC,YAAY;GAAA;KAAA;;GAAA;KAAA;KAAA,kCAexB;OACC,OAAO,IAAI,CAACC,UAAU;;;KACtB;KAAA,kCAEwBH,QAAgB,EAAEC,MAAM,EAAEG,OAAgB,EACnE;OACC,IAAI,CAACC,cAAI,CAACC,cAAc,CAACN,QAAQ,CAAC,IAAI,CAACK,cAAI,CAACE,aAAa,CAACN,MAAM,CAAC,EACjE;SACC;;OAGD,IAAMO,KAAK,GAAG,IAAI,CAACC,gBAAgB,CAACT,QAAQ,CAAC;OAE7C,IAAIQ,KAAK,GAAG,CAAC,CAAC,IAAI,CAACJ,OAAO,EAC1B;SACC;;OAGD,IAAMM,WAAW,mCAAQ,IAAI,CAACC,aAAa,GAAKV,MAAM,CAAE;OAExD,IAAIO,KAAK,KAAK,CAAC,CAAC,EAChB;SACC,IAAI,CAACL,UAAU,CAACS,IAAI,CAAC,IAAIb,YAAY,CAACC,QAAQ,EAAEU,WAAW,CAAC,CAAC;QAC7D,MAED;SACC,IAAI,CAACP,UAAU,CAACK,KAAK,CAAC,CAACK,SAAS,CAACH,WAAW,CAAC;;;;KAE9C;KAAA,8BAEoBP,UAAc,EAA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