/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
var ESeverity = /* @__PURE__ */ ((ESeverity2) => {
  ESeverity2["H"] = "H";
  ESeverity2["M"] = "M";
  ESeverity2["L"] = "L";
  return ESeverity2;
})(ESeverity || {});
var EDistressType = /* @__PURE__ */ ((EDistressType2) => {
  EDistressType2[EDistressType2["ALLIGATOR_CRACKING"] = 1] = "ALLIGATOR_CRACKING";
  EDistressType2[EDistressType2["BLEEDING"] = 2] = "BLEEDING";
  EDistressType2[EDistressType2["BLOCK_CRACKING"] = 3] = "BLOCK_CRACKING";
  EDistressType2[EDistressType2["BUMPS_SAGS"] = 4] = "BUMPS_SAGS";
  EDistressType2[EDistressType2["CORRUGATION"] = 5] = "CORRUGATION";
  EDistressType2[EDistressType2["DEPRESSION"] = 6] = "DEPRESSION";
  EDistressType2[EDistressType2["EDGE_CRACKING"] = 7] = "EDGE_CRACKING";
  EDistressType2[EDistressType2["REFLECTION_CRACKING"] = 8] = "REFLECTION_CRACKING";
  EDistressType2[EDistressType2["LANE_SHOULDER_DROP_OFF"] = 9] = "LANE_SHOULDER_DROP_OFF";
  EDistressType2[EDistressType2["LONG_TRANS_CRACKING"] = 10] = "LONG_TRANS_CRACKING";
  EDistressType2[EDistressType2["PATCHING_UTIL_CUT_PATCHING"] = 11] = "PATCHING_UTIL_CUT_PATCHING";
  EDistressType2[EDistressType2["POLISHED_AGGREGATE"] = 12] = "POLISHED_AGGREGATE";
  EDistressType2[EDistressType2["POTHOLES"] = 13] = "POTHOLES";
  EDistressType2[EDistressType2["RAILROAD_CROSSING"] = 14] = "RAILROAD_CROSSING";
  EDistressType2[EDistressType2["RUTTING"] = 15] = "RUTTING";
  EDistressType2[EDistressType2["SHOVING"] = 16] = "SHOVING";
  EDistressType2[EDistressType2["SLIPPAGE_CRACKING"] = 17] = "SLIPPAGE_CRACKING";
  EDistressType2[EDistressType2["SWELL"] = 18] = "SWELL";
  EDistressType2[EDistressType2["RAVELING"] = 19] = "RAVELING";
  EDistressType2[EDistressType2["WEATHERING"] = 20] = "WEATHERING";
  return EDistressType2;
})(EDistressType || {});
function calculateDeductValue(type, severity, density, showDebugging = false) {
  const _type = Number.parseInt(type);
  const _severity = ESeverity[severity];
  const _density = Number.parseFloat(density.toString());
  if (Number.isNaN(_density) || _density === 0) {
    return "";
  }
  const _showDebugging = showDebugging === true || showDebugging.toString().toLowerCase() === "true";
  let calc;
  switch (_type) {
    case 1:
      calc = _calcAlligator;
      break;
    case 2:
      calc = _calcBleeding;
      break;
    case 3:
      calc = _calcBlockCracking;
      break;
    case 4:
      calc = _calcBumpsSags;
      break;
    case 5:
      calc = _calcCorrugation;
      break;
    case 6:
      calc = _calcDepression;
      break;
    case 7:
      calc = _calcEdgeCracking;
      break;
    case 8:
      calc = _calcReflectionCracking;
      break;
    case 9:
      calc = _calcLaneShoulderDropOff;
      break;
    case 10:
      calc = _calcLongTransCracking;
      break;
    case 11:
      calc = _calcPatchingUtilCutPatching;
      break;
    case 12:
      calc = _calcPolishedAggregate;
      break;
    case 13:
      calc = _calcPotholes;
      break;
    case 14:
      calc = _calcRailroadCrossing;
      break;
    case 15:
      calc = _calcRutting;
      break;
    case 16:
      calc = _calcShoving;
      break;
    case 17:
      calc = _calcSlippageCracking;
      break;
    case 18:
      calc = _calcSwell;
      break;
    case 19:
      calc = _calcRaveling;
      break;
    case 20:
      calc = _calcWeathering;
      break;
  }
  if (calc) {
    const dv = calc(_severity, Math.log10(_density));
    const roundedDV = _round(dv);
    const formattedDV = `${type}|${severity}|${roundedDV}|${_density}`;
    if (_showDebugging) {
      console.log(`Deduct value: ${dv}`);
      console.log(`Rounded deduct value: ${roundedDV}`);
      console.log(`Formatted devduct value (type|severity|roundedDV|density): ${formattedDV}`);
    }
    return formattedDV;
  } else {
    return "";
  }
}
function calculatePCI(deductValues, showDebugging = false) {
  let _deductValues = deductValues.indexOf("|") > -1 ? _evaluateMultiSeverity(deductValues, showDebugging) : deductValues.split(",").map((dv) => Number.parseFloat(dv));
  const _showDebugging = showDebugging === true || showDebugging.toString().toLowerCase() === "true";
  let pci = 0;
  _deductValues = _deductValues.filter((dv) => Math.abs(dv) > 0);
  if (_deductValues.length > 0) {
    const maxCDV = _getMaxCDV(_deductValues, _showDebugging);
    if (maxCDV !== void 0) {
      pci = 100 - _round(maxCDV);
    }
  }
  return pci;
}
function _evaluateMultiSeverity(deductValues, showDebugging) {
  const updates = {};
  const _dvObject = _parseDeductValues(deductValues);
  const multiSeverityKeys = Object.keys(_dvObject).filter((k) => Object.keys(_dvObject[k]).length > 1);
  if (multiSeverityKeys.length > 0) {
    multiSeverityKeys.forEach((k) => {
      let hDeductValue = void 0;
      let mDeductValue = void 0;
      let lDeductValue = void 0;
      let hDensity = void 0;
      let mDensity = void 0;
      let lDensity = void 0;
      Object.keys(_dvObject[k]).forEach((_k) => {
        if (_k === "H") {
          hDeductValue = _dvObject[k][_k].deductValue;
          hDensity = _dvObject[k][_k].density;
        } else if (_k === "M") {
          mDeductValue = _dvObject[k][_k].deductValue;
          mDensity = _dvObject[k][_k].density;
        } else {
          lDeductValue = _dvObject[k][_k].deductValue;
          lDensity = _dvObject[k][_k].density;
        }
      });
      if (hDeductValue !== void 0 && mDeductValue !== void 0 && lDeductValue !== void 0 && hDensity !== void 0 && mDensity !== void 0 && lDensity !== void 0) {
        const func = hDensity && mDensity && lDensity ? _getThreeSeverityCaseUpdates : _getTwoSeverityCaseUpdates;
        const update = func(k, hDeductValue, mDeductValue, lDeductValue, hDensity, mDensity, lDensity, showDebugging);
        if (update) {
          updates[k] = update;
          if (showDebugging) {
            console.log("updates");
            console.log(updates);
          }
        }
      }
    });
  }
  return _getFinalDeductValues(deductValues, updates);
}
function _getThreeSeverityCaseUpdates(type, hDeductValue, mDeductValue, lDeductValue, hDensity, mDensity, lDensity, showDebugging) {
  const pci1 = calculatePCI(`${hDeductValue},${mDeductValue},${lDeductValue}`, true);
  if (showDebugging) {
    console.log("pci1");
    console.log(pci1);
  }
  const lm = lDensity + mDensity;
  const lmPCI2 = _getPCI2(type, "M", lm.toString(), hDeductValue, showDebugging);
  const mh = mDensity + hDensity;
  const mhPCI2 = _getPCI2(type, "H", mh.toString(), lDeductValue, showDebugging);
  const lh = lDensity + hDensity;
  const lhPCI2 = _getPCI2(type, "H", lh.toString(), mDeductValue, showDebugging);
  const lmh = lDensity + mDensity + hDensity;
  const lmhPCI2 = _getPCI2(type, "H", lmh.toString(), 0, showDebugging);
  if (showDebugging) {
    console.log("low med pci2");
    console.log(lmPCI2.pci);
    console.log("med high pci2");
    console.log(mhPCI2.pci);
    console.log("low high pci2");
    console.log(lhPCI2.pci);
    console.log("low med high pci2");
    console.log(lmhPCI2.pci);
  }
  const pcis = [lmPCI2, mhPCI2, lhPCI2, lmhPCI2];
  const highest = pcis.reduce((prev, cur) => {
    return cur.pci > pci1 && (!prev || cur.pci > prev.pci) ? cur : prev;
  }, void 0);
  return highest ? highest.dv : void 0;
}
function _getTwoSeverityCaseUpdates(type, hDeductValue, mDeductValue, lDeductValue, hDensity, mDensity, lDensity, showDebugging) {
  const hDV = hDeductValue || mDeductValue;
  const lDV = lDeductValue || mDeductValue;
  const pci1 = calculatePCI(`${hDV},${lDV}`, true);
  if (showDebugging) {
    console.log("pci1");
    console.log(pci1);
  }
  const hDen = hDensity || mDensity;
  const lDen = lDensity || mDensity;
  const sumDen = hDen + lDen;
  const highestSeverity = hDen === hDensity ? "H" : "M";
  const pci2 = _getPCI2(type, highestSeverity, sumDen.toString(), 0, showDebugging);
  if (showDebugging) {
    console.log("pci2");
    console.log(pci2.pci);
  }
  return pci2.pci > pci1 ? pci2.dv : void 0;
}
function _getPCI2(type, severity, density, secondDeductValue, showDebugging) {
  const deductValue = calculateDeductValue(type, severity, density);
  const splitVal = deductValue.split("|");
  const newDV = Number.parseFloat(splitVal[2]);
  return {
    pci: calculatePCI(`${splitVal[2]},${secondDeductValue}`, showDebugging),
    dv: secondDeductValue ? [newDV, secondDeductValue] : [newDV]
  };
}
function _parseDeductValues(deductValues) {
  return deductValues.split(",").reduce((prev, cur) => {
    const splitVal = cur.split("|");
    const type = splitVal[0];
    const severity = splitVal[1];
    const deductValue = Number.parseFloat(splitVal[2]);
    const density = Number.parseFloat(splitVal[3]);
    if (!prev[type]) {
      prev[type] = {};
    }
    prev[type][severity] = {
      deductValue,
      density
    };
    return prev;
  }, {});
}
function _getFinalDeductValues(deductValues, updates) {
  const skipAdditionalTypes = [];
  return deductValues.split(",").reduce((prev, cur) => {
    const splitVal = cur.split("|");
    const type = splitVal[0];
    const deductValue = Number.parseFloat(splitVal[2]);
    if (Object.keys(updates).length > 0 && updates[type]) {
      skipAdditionalTypes.push(type);
      prev.push(...updates[type]);
      delete updates[type];
    } else if (skipAdditionalTypes.indexOf(type) < 0) {
      prev.push(deductValue);
    }
    return prev;
  }, []);
}
function _calc(density, vals) {
  return vals.reduce((prev, cur, i) => {
    return prev += i === 0 ? cur : i === 1 ? cur * density : cur * Math.pow(density, i);
  }, 0);
}
function _round(v) {
  return Math.round(v * 10) / 10;
}
function _calcAlligator(severity, density) {
  const vals = severity === "H" ? [30.71, 29.22, 9.407, -6.981, -2.158, 3.047, -0.6578] : severity === "M" ? [21.62, 21.32, 5.194, -1.343, 0.2341] : [11.31, 16.05, 7.572, -1.471];
  return _calc(density, vals);
}
function _calcBleeding(severity, density) {
  const vals = severity === "H" ? [5.343, 6.927, 7.926, 4.265, -0.7582] : severity === "M" ? [2.38, 5.483, 4.128, 0.8366, 0.03659, 0.1052] : [0.01391, 0.5079, 1.576, 1.191, 0.1329, 0.03823];
  return _calc(density, vals);
}
function _calcBlockCracking(severity, density) {
  const vals = severity === "H" ? [7.926, 11.93, 8.826, 3.432, -1.382] : severity === "M" ? [2.587, 9.142, 6.647, -0.455, -0.2439, 0.1107] : [0.646, 4.002, 4.2, 0.4987, -0.06269];
  return _calc(density, vals);
}
function _calcBumpsSags(severity, density) {
  const vals = severity === "H" ? [52.77, 38.07, 8.964, -0.4148, -1.589, 2.478] : severity === "M" ? [24.66, 27.86, 15.73, 3.295, -5.27, 5.921, 7.959] : [8.768, 13.79, 7.064, 7.455, 6.041, -1.739, -1.371];
  return _calc(density, vals);
}
function _calcCorrugation(severity, density) {
  const vals = severity === "H" ? [34.21, 21.59, 1.863, 6.141, -0.687, -1.885, 0.4916] : severity === "M" ? [15.78, 19.69, 6.276, -2.124, 0.5868, 0.06045] : [2.173, 5.609, 5.976, -0.7348, -0.2172, 1.494, -0.5659];
  return _calc(density, vals);
}
function _calcDepression(severity, density) {
  const vals = severity === "H" ? [17.84, 5.904, 9.406, 18.28, -2.753, -7.798, 2.402] : severity === "M" ? [8.471, 3.171, 10.25, 12.2, -0.9687, -4.601, 1.079] : [4.836, -0.7572, 4.786, 12.39, 1.233, -4.871, 0.9749];
  return _calc(density, vals);
}
function _calcEdgeCracking(severity, density) {
  const vals = severity === "H" ? [14.99, 13.5, 10.62, 5.15, -1.792, -2.826] : severity === "M" ? [9.177, 10.26, 5.704, -0.6812, -0.8588, 0.2123] : [3.049, 4.802, 4.058, -0.3556, -1.55, 1.02];
  return _calc(density, vals);
}
function _calcReflectionCracking(severity, density) {
  const vals = severity === "H" ? [14.99, 17.21, 27.63, 31.13, -20.94, -18.82, 9.985] : severity === "M" ? [9.9193, 10.88, 8.073, 14.8, -3.792, -9.583, 3.651] : [2.356, 6.664, 3.717, -1.393, 1.94, 1.785, -0.9707];
  return _calc(density, vals);
}
function _calcLaneShoulderDropOff(severity, density) {
  const vals = severity === "H" ? [7.549, 6.907, 7.284, 9.304, 2.064] : severity === "M" ? [4.02, 1.744, 14.36, 15.92, -40.22, 23.6] : [2.004, 1.065, 9.706, 11.75, -27.52, 15.04];
  return _calc(density, vals);
}
function _calcLongTransCracking(severity, density) {
  const vals = severity === "H" ? [19.56, 24.06, 12.12, 9.336, 1.532, -4.396] : severity === "M" ? [9.751, 15.53, 4.719, 1.369, 1.206, -1.164] : [2.347, 9.074, 8.424, -1.338, -1.873, 1.144];
  return _calc(density, vals);
}
function _calcPatchingUtilCutPatching(severity, density) {
  const vals = severity === "H" ? [20.15, 16.55, 5.415, 10.77, 3.257, -4.502] : severity === "M" ? [10.28, 12.71, 6.174, 1.928, 0.6923, -0.4673] : [2.523, 6.892, 5.702, 2.407, 0.2185, -0.8722];
  return _calc(density, vals);
}
function _calcPolishedAggregate(severity, density) {
  const vals = severity === "H" || severity === "M" || severity === "L" ? [3.417, -26.82, 71.15, -76.22, 38.52, -6.904] : [];
  return _calc(density, vals);
}
function _calcPotholes(severity, density) {
  const vals = severity === "H" ? [110.7, 60.66, 2.378, -2.664] : severity === "M" ? [90.47, 60.41, -0.1123, -4.746] : [58.19, 40.53, 2.884, -1.443, 0.1195];
  return _calc(density, vals);
}
function _calcRailroadCrossing(severity, density) {
  const vals = severity === "H" ? [20, 38.64, -62.82, 193.6, -162.7, 41.22] : severity === "M" ? [6.994, 23.47, -52.3, 167.3, -143.6, 37.64] : [1.998, -0.7488, 13.8, -0.7917, -1.981];
  return _calc(density, vals);
}
function _calcRutting(severity, density) {
  const vals = severity === "H" ? [27.61, 25.19, 8.557, 1.65, -2.203] : severity === "M" ? [18.47, 20.77, 6.617, -1.13, -2.286] : [8.833, 14.84, 3.129, 0.1451, 2.438, -1.279];
  return _calc(density, vals);
}
function _calcShoving(severity, density) {
  const vals = severity === "H" ? [18.7, 20.17, 10.3, 4.694, 1.331, -2.61] : severity === "M" ? [10.39, 14.78, 5.488, 4.001, 3.23, -2.387] : [4.002, 10.66, 6.332, -0.5226, -0.1923];
  return _calc(density, vals);
}
function _calcSlippageCracking(severity, density) {
  const vals = severity === "H" ? [19.73, 32.21, 25.87, -2.871, -10.55, 1.496, 0.7393] : severity === "M" ? [11.38, 18.55, 18.38, 1.628, -7.596, 0.5841, 0.5946] : [5.144, 12.95, 11.95, -0.833, -2.995, 1.325, -0.2113];
  return _calc(density, vals);
}
function _calcSwell(severity, density) {
  const vals = severity === "H" ? [34, 16.84, -23.09, 41.17, -14.84] : severity === "M" ? [12, 15.67, 20.59, -28.33, 21, -5.508] : [1.995, 11.9, -12.83, 16.74, -5.361];
  return _calc(density, vals);
}
function _calcRaveling(severity, density) {
  const vals = severity === "H" ? [16.77, 10.67, 5.758, 12.9, -0.0633, -5.659, 1.48] : severity === "M" ? [8.512, 5.106, 1.63, 3.469, 1.028, -1.211, 0.1938] : [];
  return _calc(density, vals);
}
function _calcWeathering(severity, density) {
  const vals = severity === "H" ? [3.673, 3.349, 2.598, -1.233, 2.754, 2.434, -1.485] : severity === "M" ? [1.12, 0.9769, 0.93, -0.5955, 1.041, 1.111, -0.614] : [-0.0386, -0.0736, 0.834, 1.63, -0.0287, -0.639, 0.1515];
  return _calc(density, vals);
}
function _getMaxCDV(deductValues, showDebugging) {
  let maxCDV;
  const valuesGreaterThan2 = deductValues.filter((deductValue) => deductValue > 2);
  if (valuesGreaterThan2.length <= 1) {
    maxCDV = valuesGreaterThan2.reduce((prev, cur) => {
      prev += cur;
      return prev;
    }, 0);
    if (showDebugging) {
      console.log("If none or only one individual deduct value is greater");
      console.log("than two, the total value is used in place of the maximum CD");
      console.log(`Max CDV: ${maxCDV}`);
    }
  } else {
    const sortedDVs = deductValues.sort((a, b) => b - a);
    const highestDV = sortedDVs[0];
    const m = 1 + 9 / 98 * (100 - highestDV);
    if (showDebugging) {
      console.log(`Deduct values sorted in descending order: ${sortedDVs}`);
      console.log(`Highest deduct value: ${highestDV}`);
      console.log(`Allowable number of deducts including fractions: ${m}`);
    }
    if (m <= 10) {
      const vals = _reduceDeductValues(sortedDVs, m);
      if (showDebugging) {
        console.log(`Reduce deduct values to the m largest including the fractional part: ${vals}`);
      }
      const cdvs = _getCDVs(vals, showDebugging);
      maxCDV = Math.max(...cdvs);
      if (showDebugging) {
        console.log(`Max CDV: ${maxCDV}`);
      }
    } else {
      if (showDebugging) {
        console.log("Allowable number of deducts including fractions (must be less than or equal to ten)");
      }
    }
  }
  return maxCDV;
}
function _reduceDeductValues(sortedDVs, m) {
  const fractionalPart = m - Math.floor(m);
  return sortedDVs.length < m ? sortedDVs : sortedDVs.reduce((prev, cur, i) => {
    if (i + 1 < m) {
      prev.push(cur);
    } else if (fractionalPart > 0) {
      prev.push(_round(cur * fractionalPart));
    }
    return prev;
  }, []);
}
function _getCDVs(vals, showDebugging) {
  let len = vals.length;
  const cdvs = [];
  while (len >= 1) {
    if (showDebugging) {
      console.log(`vals: ${vals}`);
    }
    cdvs.push(_getCDV(vals, showDebugging));
    len -= 1;
    vals.splice(len, 1, 2);
  }
  return cdvs;
}
function _getCDV(vals, showDebugging) {
  const totalDV = vals.reduce((prev, cur) => prev += cur, 0);
  const q = vals.reduce((prev, cur) => cur > 2 ? prev + 1 : prev, 0);
  return _calcCDV(totalDV, q, showDebugging);
}
function _calcCDV(total, q, showDebugging) {
  const vals2 = {
    7: [-9.482, 0.6045, 3563e-7, -5448e-9],
    6: [-8.165, 0.5724, 4266e-7, -4254e-9],
    5: [-7.932, 0.6091, 3464e-7, -4265e-9],
    4: [-6.78, 0.6301, 7376e-7, -6357e-9],
    3: [-5.015, 0.7546, -43e-5, -3075e-9],
    2: [-1.668, 0.7992, -3013e-7, -5214e-9],
    1: [1651e-18, 1, -4249e-21, 1149e-22]
  };
  const cdv = _calc(total, vals2[q >= 7 ? 7 : q]);
  if (showDebugging) {
    console.log(`totalDV: ${total}`);
    console.log(`q: ${q}`);
    console.log(`CDV: ${cdv}`);
  }
  return cdv;
}
export {
  EDistressType as E,
  ESeverity as a,
  calculatePCI as b,
  calculateDeductValue as c
};
