All files / src/pages/flowsheet-page/economics/cost-items-panel/unit-costs/category-group bulkSetupHelpers.ts

13.41% Statements 11/82
6.38% Branches 3/47
28.57% Functions 2/7
15.62% Lines 10/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200                                                    76x 76x 76x 76x                       23x           6x 6x 5x 5x 5x                                                                                                                                                                                                                                                                                                        
import type {
  BulkEquipmentDriverInputResult,
  BulkEquipmentDriverInputSetup,
  BulkEquipmentSetupResult,
  CapitalCostLineRead,
  CostCurveRead,
} from "@/api/apiStore.gen";
import {
  SourceCa8Enum as DriverInputSourceEnum,
  ModeEnum,
  SourceOptionsEnum,
} from "@/api/apiStore.gen";
import {
  defaultManualValueForSpec,
  type RequiredDriverSpec,
  requiredDriverSpecsFromCurve,
} from "../../../cost-curves/model/driverSpecs";
import {
  costDriverSummaryLabel,
  curveSelectableForMapping,
  generatedCapitalLineForCostableItem,
  relevantCurvesForMapping,
} from "../../model/capitalLineHelpers";
import type { UnitCostRow } from "../../model/capitalLineTypes";
import { rowHasSizingValue } from "../../model/unitCostGrouping";
 
export enum BulkTargetMode {
  Missing = "missing",
  Included = "included",
  Custom = "custom",
}
 
export type BulkDriverInputMode = ModeEnum;
 
export type BulkApplyState =
  | { kind: "idle" }
  | { kind: "saving"; message: string }
  | { kind: "saved"; message: string }
  | { kind: "error"; message: string };
 
export function rowCanReceiveBulkSetup(row: UnitCostRow) {
  return Boolean(
    row.costableItem?.included && row.costableItem.equipment_mapping,
  );
}
 
export function curveCompatibleWithRow(curve: CostCurveRead, row: UnitCostRow) {
  const mapping = row.costableItem?.equipment_mapping;
  if (!mapping) return false;
  Iif (!curveSelectableForMapping(curve)) return false;
  return relevantCurvesForMapping(mapping, [curve]).some(
    (matchingCurve) => matchingCurve.id === curve.id,
  );
}
 
export function currentCostCurveLabel(
  row: UnitCostRow,
  curves: CostCurveRead[],
) {
  const mapping = row.costableItem?.equipment_mapping;
  Iif (!mapping) return "No mapping";
  Iif (!mapping.cost_curve) return "No cost curve";
  return (
    curves.find((curve) => curve.id === mapping.cost_curve)?.name ||
    "Selected curve"
  );
}
 
export function currentCostDriverLabel(
  row: UnitCostRow,
  capitalLines: CapitalCostLineRead[],
) {
  const driver = row.costableItem?.cost_driver;
  return costDriverSummaryLabel(
    driver,
    generatedCapitalLineForCostableItem(capitalLines, row.costableItem),
  );
}
 
export function buildDriverInputSetup(
  curve: CostCurveRead,
  modes: Record<string, BulkDriverInputMode>,
  manualValues: Record<string, string>,
): Record<string, BulkEquipmentDriverInputSetup> {
  return Object.fromEntries(
    requiredDriverSpecsFromCurve(curve).map((spec) => {
      const mode = driverInputModeForSpec(spec, modes);
      return [
        spec.key,
        {
          mode,
          manual_value:
            mode === ModeEnum.Manual
              ? (
                  manualValues[spec.key] ?? defaultManualValueForSpec(spec)
                ).trim()
              : "",
        },
      ];
    }),
  );
}
 
export function driverInputModeForSpec(
  spec: RequiredDriverSpec,
  modes: Record<string, BulkDriverInputMode>,
): BulkDriverInputMode {
  const selectedMode = modes[spec.key];
  Iif (selectedMode) return selectedMode;
  if (spec.sourceOptions.includes(SourceOptionsEnum.Property)) {
    return ModeEnum.AutoProperty;
  }
  if (spec.sourceOptions.includes(SourceOptionsEnum.Manual)) {
    return ModeEnum.Manual;
  }
  return ModeEnum.Keep;
}
 
export function rowHasRequiredDriverInputs(
  row: UnitCostRow,
  curve: CostCurveRead,
  capitalLines: CapitalCostLineRead[],
) {
  const specs = requiredDriverSpecsFromCurve(curve);
  Iif (specs.length === 0) return rowHasSizingValue(row);
  const capitalLine = generatedCapitalLineForCostableItem(
    capitalLines,
    row.costableItem,
  );
  const inputs = capitalLine?.driver_inputs ?? {};
  return specs.every((spec) => {
    const input = inputs[spec.key];
    Iif (!input) return false;
    if (input.source === DriverInputSourceEnum.Property) {
      return Boolean(input.property_info);
    }
    if (input.source === DriverInputSourceEnum.Manual) {
      return Boolean(input.manual_value?.trim());
    }
    return false;
  });
}
 
export function currentDriverInputLabel(
  capitalLine: CapitalCostLineRead | undefined,
  spec: RequiredDriverSpec,
) {
  const input = capitalLine?.driver_inputs?.[spec.key];
  Iif (!input || !input.source) return "Not selected";
  Iif (input.source === DriverInputSourceEnum.Property)
    return "Selected property";
  if (input.source === DriverInputSourceEnum.Manual) {
    return input.manual_value
      ? `Manual ${input.manual_value} ${spec.unit}`
      : "Manual value";
  }
  return "Not selected";
}
 
export function driverInputResultLabel(result: BulkEquipmentDriverInputResult) {
  if (result.status === "ready" && result.property_label) {
    const scope = sizingScopeLabel(result.property_scope);
    const unit = result.property_unit ? ` (${result.property_unit})` : "";
    return `${scope ? `${scope} ` : ""}${result.property_label}${unit}`;
  }
  if (result.status === "ready" && result.manual_value) {
    return `Manual ${result.manual_value}${result.unit ? ` ${result.unit}` : ""}`;
  }
  Iif (result.status === "preserved") return "Keep existing";
  Iif (result.status === "no_recommendation") return "No compatible property";
  Iif (result.status === "missing") return "Missing value";
  Iif (result.status === "no_driver") return "No cost driver";
  Iif (result.status === "unsupported") return "Unsupported";
  return result.message || result.status;
}
 
export function bulkDriverInputSummary(results: BulkEquipmentSetupResult[]) {
  const summaries = new Map<string, string>();
  results.forEach((result) => {
    Object.values(result.driver_input_results ?? {}).forEach((inputResult) => {
      Iif (inputResult.status !== "ready") return;
      const summary = driverInputResultLabel(inputResult).toLocaleLowerCase();
      summaries.set(`${inputResult.key}|${summary}`, summary);
    });
  });
  return formatList([...summaries.values()]);
}
 
function sizingScopeLabel(scope: string) {
  Iif (scope === "input_stream") return "input stream";
  Iif (scope === "output_stream") return "output stream";
  Iif (scope === "unit") return "unit";
  return scope.replace(/_/g, " ").trim();
}
 
function formatList(values: string[]) {
  Iif (values.length <= 2) return values.join(" and ");
  return `${values.slice(0, -1).join(", ")}, and ${values[values.length - 1]}`;
}