"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@babel/runtime/helpers/defineProperty"),e=require("corva-convert-units"),r=require("lodash"),n=require("./constants/units.js");function i(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var o=i(t),u=i(e);function s(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function a(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?s(Object(r),!0).forEach((function(e){o.default(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):s(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var l={imperial:n.IMPERIAL_UNITS,metric:n.METRIC_UNITS,custom:a({},n.IMPERIAL_UNITS)},c=[],p={};var f=function(t){return l.custom[t]},v=function(t){var e,i=n.UNIT_TYPES_DEFINITIONS_MAP[t];return i?r.sortBy(u.default().list(i.origin),"abbr"):(null===(e=p[t])||void 0===e?void 0:e.measures)||[]},d=function(t){var e,r,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,o=null===(e=n.UNIT_TYPES_DEFINITIONS_MAP[t])||void 0===e?void 0:e.origin,s=u.default(void 0,o);if(null===i&&(i=l.custom[t]),s.getUnit(i))return s.describe(i).display;var a=null===(r=v(t).find((function(t){return t.measure===i})))||void 0===r?void 0:r.display;return a||i},m=function(t,e,r){var i,o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null,s=arguments.length>4&&void 0!==arguments[4]?arguments[4]:2;if(!t||!r||r===o||p[e])return t;if(null===o&&(!(o=f(e))||r===o))return t;var a=null===(i=n.UNIT_TYPES_DEFINITIONS_MAP[e])||void 0===i?void 0:i.origin,l=u.default(t,a).from(r).to(o);return Number.isFinite(l)?Math.round(l*Math.pow(10,s))/Math.pow(10,s):l};exports.convertArray=function(){for(var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],e=arguments.length>1?arguments[1]:void 0,r=arguments.length>2?arguments[2]:void 0,n=arguments.length>3?arguments[3]:void 0,i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:null,o=arguments.length>5&&void 0!==arguments[5]?arguments[5]:2,u=0;u<t.length;u+=1)t[u][e]=m(t[u][e],r,n,i,o);return t},exports.convertImmutables=function(t,e,r,n){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:null,o=arguments.length>5&&void 0!==arguments[5]?arguments[5]:2;return t.map((function(t){var u=m(t.get(e),r,n,i,o);return t.set(e,u)}))},exports.convertImmutablesByBatch=function(t,e){return t.map((function(t){return e.reduce((function(t,e){var r=e.key,n=e.unitType,i=e.from,o=e.to,u=e.precision,s=t.get(r),a=m(s,n,i,o,u);return t.set(r,a)}),t)}))},exports.convertValue=m,exports.getAllUnitTypes=function(){return n.UNIT_DEFINITIONS},exports.getDefaultImperialUnit=function(t){return l.imperial[t]},exports.getDefaultUnits=function(){return l.custom},exports.getUniqueUnitsByType=function(t){var e=v(t);return r.uniqBy(e,"abbr")},exports.getUnitDescription=function(t){var e,r=null===(e=n.UNIT_TYPES_DEFINITIONS_MAP[t])||void 0===e?void 0:e.origin;return u.default(void 0,r).describe(t)},exports.getUnitDisplay=d,exports.getUnitPlural=function(t){var e,r=null===(e=n.UNIT_TYPES_DEFINITIONS_MAP[t])||void 0===e?void 0:e.origin;return p[t]?d(t):u.default(void 0,r).describe(f(t)).plural},exports.getUnitPreference=f,exports.getUnitSingular=function(t){var e,r=null===(e=n.UNIT_TYPES_DEFINITIONS_MAP[t])||void 0===e?void 0:e.origin;return p[t]?d(t):u.default(void 0,r).describe(f(t)).singular},exports.getUnitSystem=function(){return"custom"},exports.getUnitsByType=v,exports.getUserUnits=function(){return l.custom},exports.subscribeForUserUnitsUpdates=function(t){return c.push(t),function(){c=c.filter((function(e){return e!==t}))}},exports.updateUserUnits=function(t){var e,i,o=t.userUnits,u=void 0===o?{}:o,s=t.companyUnits,f=void 0===s?{}:s,v=t.companyCustomUnitSystem,d=void 0===v?{}:v,m=t.userCustomUnitSystem,g=void 0===m?{}:m,I=t.companyCustomUnits;l.custom=a(a(a(a(a({},n.IMPERIAL_UNITS),f),d),u),g),I&&(e=I,p=r.mapValues(e||{},(function(t,e){var r=t.measures;return{display:t.name,imperial:e,measures:r.map((function(t){return{abbr:t.abbr,display:t.name,measure:t.abbr,plural:t.name,singular:t.name,system:"custom"}})),metric:e,origin:e,type:e}}))),i=l.custom,c.forEach((function(t){return t(i)}))};
//# sourceMappingURL=convert.js.map
