<commit_before>import BungieImage from 'app/dim-ui/BungieImage';
import { t } from 'app/i18next-t';
import { showItemPicker } from 'app/item-picker/item-picker';
import Sockets from 'app/loadout/loadout-ui/Sockets';
import { useD2Definitions } from 'app/manifest/selectors';
import { AppIcon, faRandom, lockIcon } from 'app/shell/icons';
import { DestinyEnergyType } from 'bungie-api-ts/destiny2';
import clsx from 'clsx';
import { PlugCategoryHashes } from 'data/d2/generated-enums';
import _ from 'lodash';
import React, { Dispatch } from 'react';
import { DimItem, PluggableInventoryItemDefinition } from '../../inventory/item-types';
import { LoadoutBuilderAction } from '../loadout-builder-reducer';
import LoadoutBuilderItem from '../LoadoutBuilderItem';
import styles from './GeneratedSetItem.m.scss';

/**
 * Shows how we recommend the energy of this armor be changed in order to fit its mods.
 */
function EnergySwap({
  item,
  assignedMods,
}: {
  item: DimItem;
  assignedMods?: PluggableInventoryItemDefinition[];
}) {
  const defs = useD2Definitions()!;

  const armorEnergyCapacity = item.energy?.energyCapacity || 0;
  const armorEnergy = defs.EnergyType.get(item.energy!.energyTypeHash);

  const modCost = _.sumBy(assignedMods, (mod) => mod.plug.energyCost?.energyCost || 0);
  const modEnergyHash = assignedMods?.find(
    (mod) => mod.plug.energyCost?.energyType !== DestinyEnergyType.Any
  )?.plug.energyCost?.energyTypeHash;
  const modEnergy = (modEnergyHash && defs.EnergyType.get(modEnergyHash)) || null;

  const resultingEnergy = modEnergy ?? armorEnergy;
  let resultingEnergyCapacity = armorEnergyCapacity;

  if (modEnergyHash === armorEnergy.hash) {
    resultingEnergyCapacity = Math.max(armorEnergyCapacity, modCost);
  } else if (modEnergy) {
    resultingEnergyCapacity = modCost;
  }

  const noEnergyChange =
    resultingEnergyCapacity === armorEnergyCapacity && resultingEnergy === armorEnergy;

  return (
    <div className={clsx(styles.energySwapContainer, { [styles.energyHidden]: noEnergyChange })}>
      <div className={styles.energyValue}>
        <div className={clsx({ [styles.masterworked]: armorEnergyCapacity === 10 })}><commit_msg>Fix energy swap display in LO<commit_after>import BungieImage from 'app/dim-ui/BungieImage';
import { t } from 'app/i18next-t';
import { showItemPicker } from 'app/item-picker/item-picker';
import Sockets from 'app/loadout/loadout-ui/Sockets';
import { useD2Definitions } from 'app/manifest/selectors';
import { AppIcon, faRandom, lockIcon } from 'app/shell/icons';
import { DestinyEnergyType } from 'bungie-api-ts/destiny2';
import clsx from 'clsx';
import { PlugCategoryHashes } from 'data/d2/generated-enums';
import _ from 'lodash';
import React, { Dispatch } from 'react';
import { DimItem, PluggableInventoryItemDefinition } from '../../inventory/item-types';
import { LoadoutBuilderAction } from '../loadout-builder-reducer';
import LoadoutBuilderItem from '../LoadoutBuilderItem';
import styles from './GeneratedSetItem.m.scss';

/**
 * Shows how we recommend the energy of this armor be changed in order to fit its mods.
 */
function EnergySwap({
  item,
  assignedMods,
}: {
  item: DimItem;
  assignedMods?: PluggableInventoryItemDefinition[];
}) {
  const defs = useD2Definitions()!;

  const armorEnergyCapacity = item.energy?.energyCapacity || 0;
  const armorEnergy = defs.EnergyType.get(item.energy!.energyTypeHash);

  const modCost = _.sumBy(assignedMods, (mod) => mod.plug.energyCost?.energyCost || 0);
  const modEnergyHash = assignedMods?.find(
    (mod) => mod.plug.energyCost?.energyType !== DestinyEnergyType.Any
  )?.plug.energyCost?.energyTypeHash;
  const modEnergy = (modEnergyHash && defs.EnergyType.get(modEnergyHash)) || null;

  // The armor energy type and capacity needed for the mods
  const resultingEnergy = modEnergy ?? armorEnergy;
  let resultingEnergyCapacity = armorEnergyCapacity;

  // If there is a non Any mod energy type and its different to ther armor energy type
  // we always use the mod cost as we are swapping energy types
  if (modEnergyHash && modEnergyHash !== armorEnergy.hash) {
    resultingEnergyCapacity = modCost;
  } else {
    // Otherwise we just take the max of capacity and mod cost
    resultingEnergyCapacity = Math.max(armorEnergyCapacity, modCost);
  }

  const noEnergyChange =
    resultingEnergyCapacity === armorEnergyCapacity && resultingEnergy === armorEnergy;

  return (
    <div className={clsx(styles.energySwapContainer, { [styles.energyHidden]: noEnergyChange })}>
      <div className={styles.energyValue}>
        <div className={clsx({ [styles.masterworked]: armorEnergyCapacity === 10 })}>
{'fraction_non_alphanumeric': 0.12317596566523605, 'fraction_numerical': 0.004291845493562232, 'mean_word_length': 4.840852130325815, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3630868', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1378, 'n_words': 358}