"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/toConsumableArray"),t=require("@babel/runtime/helpers/defineProperty"),r=require("lodash/isNumber"),a=require("lodash/get"),i=require("lodash/has"),n=require("lodash/set"),d=require("moment"),u=require("./convert.js");function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=s(e),l=s(t),p=s(r),c=s(a),f=s(i),v=s(n),y=s(d);function g(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,a)}return r}function m(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?g(Object(r),!0).forEach((function(t){l.default(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):g(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var h=function(e){return 0!==e&&!e},_=function(e,t,r,a){return h(e)?!!a:p.default(e)&&e>t&&e<r},b=function(e,t,r){return p.default(e)?t&&r?u.convertValue(e,t,r):parseFloat(e):null},O=function(e,t,r){var a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:5;return p.default(e)?t&&r?u.convertValue(e,t,u.getUnitPreference(t),r,a):parseFloat(e):null};exports.convertToImperialUnit=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],r=e,a=c.default(e,["data","date_str"]);if(a){var i=y.default(new Date(a));i.isValid()&&(r=v.default(r,["data","date"],i.unix()))}var n=O(c.default(e,["data","mud_density"]),"density","ppg"),d=O(c.default(e,["data","depth"]),"length","ft"),u=O(c.default(e,["data","tvd"]),"length","ft"),s=m(m({},r),{},{data:m(m({},r.data),{},{mud_density:n,depth:d,tvd:u,date_str:null})});return s=t.reduce((function(t,r){var a=["data"].concat(o.default(r.key.split(".")));if(!f.default(e,a))return t;var i=O(c.default(e,a),r.unitType,r.unit);return v.default(t,a,i)}),s)},exports.convertToPrefUnit=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],r=b(c.default(e,["data","mud_density"]),"density","ppg"),a=b(c.default(e,["data","depth"]),"length","ft"),i=b(c.default(e,["data","tvd"]),"length","ft"),n=m(m({},e),{},{data:m(m({},e.data),{},{mud_density:r,depth:a,tvd:i})});return n=t.reduce((function(t,r){var a=["data"].concat(o.default(r.key.split(".")));if(!f.default(e,a))return t;var i=b(c.default(e,a),r.unitType,r.unit);return v.default(t,a,i)}),n)},exports.ctoi=O,exports.ctop=b,exports.isValueEmpty=h,exports.isValueValid=_,exports.validate=function(e){var t=e.data,r=t.date_str,a=t.mud_density,i=t.mud_type,n=t.viscosity,d=n.pv,u=n.yp,s=n.rpm_readings,o=!1,l={viscosity:{rpm_readings:[]}};r&&!y.default(new Date(r)).isValid()&&(o=!0,l.date="Invalid Date."),i||(o=!0,l.mud_type="Mud Type is required");var p=b(5,"density","ppg"),c=b(30,"density","ppg");_(a,p,c,!1)||(o=!0,l.mud_density="It should be ".concat(p," ~ ").concat(c)),(h(d)||h(u))&&s.length<2&&(o=!0,l.viscosity.rpm_readings_required="At least 2 paris of rpm and dial_reading required."),_(d,0,200,!l.viscosity.rpm_readings_required)||(o=!0,l["viscosity.pv"]="It should be 0~200"),_(u,0,200,!l.viscosity.rpm_readings_required)||(o=!0,l["viscosity.yp"]="It should be 0~200");for(var f=[],v=0;v<s.length;v+=1){l.viscosity.rpm_readings[v]={};var g=l.viscosity.rpm_readings[v];_(s[v].rpm,0,1e3)||(o=!0,g.rpm="It should be 0~1000"),_(s[v].dial_reading,0,300)||(o=!0,g.dial_reading="It should be 0~300"),-1!==f.indexOf(s[v].rpm)&&(o=!0,g.rpm="Duplicated RPM value"),f.push(s[v].rpm),s[v].originalIndex=v}var m=s.sort((function(e,t){return e.rpm-t.rpm}));return m.forEach((function(e,t){var r=l.viscosity.rpm_readings[e.originalIndex];m[t-1]&&e.dial_reading<m[t-1].dial_reading&&(o=!0,r.dial_reading="Dial Readings should be in increasing order")})),{hasErrors:o,errors:l}};
