'use strict';

/*!
 * @techmely/utils
 * Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com>
 * MIT Licensed
 */


// src/numberCreateOperationPrecision.ts
function createOperationPrecision(operation) {
  return (...nums) => {
    const [first, ...others] = nums;
    return others.reduce((prev, next) => operation(prev, next), first);
  };
}

// src/numberDigitLengthPrecision.ts
function digitLengthPrecision(num) {
  const eSplit = num.toString().split(/[eE]/);
  const len = (eSplit[0].split(".")[1] || "").length - +(eSplit[1] || 0);
  return len > 0 ? len : 0;
}
function checkBoundaryPrecision(num) {
  {
    if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
      console.warn(
        `${num} is beyond boundary when transfer to integer, the results may not be accurate`
      );
    }
  }
}

// src/numberStripPrecision.ts
function stripPrecision(num, precision = 15) {
  return +parseFloat(Number(num).toPrecision(precision));
}

// src/numberFloat2FixedPrecision.ts
function float2FixedPrecision(num) {
  if (num.toString().indexOf("e") === -1) {
    return Number(num.toString().replace(".", ""));
  }
  const dLen = digitLengthPrecision(num);
  const powDLen = 10 ** dLen;
  return dLen > 0 ? stripPrecision(Number(num) * powDLen) : Number(num);
}

// src/numberTimesPrecision.ts
var timesPrecision = createOperationPrecision((num1, num2) => {
  const num1Changed = float2FixedPrecision(num1);
  const num2Changed = float2FixedPrecision(num2);
  const baseNum = digitLengthPrecision(num1) + digitLengthPrecision(num2);
  const leftValue = num1Changed * num2Changed;
  checkBoundaryPrecision(leftValue);
  const baseNumPow = 10 ** baseNum;
  return leftValue / baseNumPow;
});

// src/numberMinusPrecision.ts
var minusPrecision = createOperationPrecision((num1, num2) => {
  const baseNum = 10 ** Math.max(digitLengthPrecision(num1), digitLengthPrecision(num2));
  return (timesPrecision(num1, baseNum) - timesPrecision(num2, baseNum)) / baseNum;
});

exports.minusPrecision = minusPrecision;
