export function normalizeWarehouseType(value) {
  return String(value || "").trim().toUpperCase();
}

function numericValue(value) {
  const numberValue = Number(value || 0);
  return Number.isFinite(numberValue) ? numberValue : 0;
}

export function specialAdjustmentDirectionLabel(direction) {
  return String(direction || "").toUpperCase() === "OUT" ? "特殊出库" : "特殊入库";
}

export function specialAdjustmentMeasureMode(warehouseType, row = {}) {
  const normalizedWarehouseType = normalizeWarehouseType(warehouseType);
  if (["RAW", "SCRAP"].includes(normalizedWarehouseType)) {
    return "WEIGHT";
  }
  if (normalizedWarehouseType === "AUX") {
    return "QTY";
  }
  if (["FINISHED", "RETURN"].includes(normalizedWarehouseType)) {
    return "QTY_WITH_WEIGHT";
  }
  if (normalizedWarehouseType === "SEMI") {
    const qty = numericValue(row.remaining_qty) || numericValue(row.qty_available);
    const weight = numericValue(row.remaining_weight_kg) || numericValue(row.weight_available_kg);
    if (qty > 0 && weight <= 0) {
      return "QTY";
    }
    if (weight > 0 && qty <= 0) {
      return "WEIGHT";
    }
    return "SEMI_SELECT";
  }
  return "QTY_WITH_WEIGHT";
}

export function specialAdjustmentOperationKeys() {
  return {
    raw: {
      in: "raw-special-in",
      out: "raw-special-out"
    },
    semi: {
      in: "semi-special-in",
      out: "semi-special-out"
    },
    finished: {
      in: "finished-special-in",
      out: "finished-special-out"
    },
    auxiliary: {
      in: "aux-special-in",
      out: "aux-special-out"
    },
    scrap: {
      in: "scrap-special-in",
      out: "scrap-special-out"
    },
    return: {
      in: "return-special-in",
      out: "return-special-out"
    }
  };
}
