text: import { createSelector } from 'reselect';
  import formatPosition from '../../helpers/positionFormatter';
  import config from '../../config';
  import { marginTrade } from '../../constants/tradingStyleConstants';
  import { getInstrumentDecimals } from 'helpers/instrumentHelper';
  
  const getInstrumentsWithProductFullNamesAndProductSymbols = (
    instrument,
    product
  ) =>
    instrument.instruments.map(instrument => {
      const product1 = product.products.find(
        product => product.ProductId === instrument.Product1
      );
      const product2 = product.products.find(
        product => product.ProductId === instrument.Product2
      );
      const { Product: Product1Symbol, ProductFullName: Product1FullName } =
        product1 || {};
      const { Product: Product2Symbol, ProductFullName: Product2FullName } =
        product2 || {};
  
      return {
        ...instrument,
        Product1FullName,
        Product2FullName,
        Product1Symbol,
        Product2Symbol
      };
    });
  
  export const standardInstrumentsWithProductFullNamesAndProductSymbols = createSelector(
    state => state.instrument,
    state => state.product,
  
    (instrument, product) => {
      return getInstrumentsWithProductFullNamesAndProductSymbols(
        instrument,
        product
      );
    }
  );
  
  const marginInstrumentsWithProductFullNamesAndProductSymbols = createSelector(
    state => state.margin.instrument,
    state => state.product,
  
    (instrument, product) => {
      return getInstrumentsWithProductFullNamesAndProductSymbols(
        instrument,
        product
      );
    }
  );
  
  export const instrumentsWithProductFullNamesAndProductSymbols = createSelector(
    state => state.user.userInfo.MarginBorrowerEnabled,
    state => state.form.tradingStyle,
    state => state.margin,
    state => state,
    (marginEnabled, formObj, margin, state) => {
      let tradingStyle;
      if (formObj) {
        tradingStyle = formObj.values.tradingStyle;
      }
  
      if (
        margin &&
        marginEnabled &&
        !margin.instrument.fetching &&
        tradingStyle === marginTrade
      ) {
        return marginInstrumentsWithProductFullNamesAndProductSymbols(state);
      }
  
      return standardInstrumentsWithProductFullNamesAndProductSymbols(state);
    }
  );
  
  export const instrumentSelectorInstruments = createSelector(
    instrumentsWithProductFullNamesAndProductSymbols,
    () => config,
    (instruments, config) => {
      if (config.InstrumentSelector.instruments.length === 0) {
        return instruments;
      }
  
      return instruments.filter(instrument =>
        config.InstrumentSelector.instruments.includes(instrument.Symbol)
      );
    }
  );
  
  export const instrumentInConfigSelector = createSelector(
    instrumentsWithProductFullNamesAndProductSymbols,
    // This list of instruments seems to be filtered already...
    // So filtering out instruments without both products available instead.
    instruments => {
      const filtered = instruments.filter(
        inst =>
          inst.Product1Symbol !== undefined && inst.Product2Symbol !== undefined
      );
      return filtered;
    }
  );
  
  export const selectedInstrumentSelector = createSelector(
    [
      state => state.user.userInfo.MarginBorrowerEnabled,
      state => state.form.tradingStyle,
      state => state.margin,
      instrumentsWithProductFullNamesAndProductSymbols,
      state => state.instrument.selectedInstrumentId
    ],
    (marginEnabled, formObj, margin, instruments, instrumentId) => {
      let tradingStyle;
      if (formObj) {
        tradingStyle = formObj.values.tradingStyle;
      }
      let id = instrumentId;
      if (
        margin &&
        marginEnabled &&
        !margin.instrument.fetching &&
        tradingStyle === marginTrade
      ) {
        id = margin.instrument.selectedInstrumentId;
      }
      return instruments.find(i => i.InstrumentId === id) || {};
    }
  );
  
  export const selectedInstrumentDecimalPlacesSelector = createSelector(
    [selectedInstrumentSelector],
    getInstrumentDecimals
  );
  
  export const instrumentsNotStoppedSelector = createSelector(
    instrumentsWithProductFullNamesAndProductSymbols,
    instruments =>
      instruments.filter(instrument => {
        return instrument.SessionStatus !== 'Stopped';
      })
  );
  
  export const getInstrumentById = (state, id) =>
    state.instrument.instruments.find(inst => inst.InstrumentId === id);
  
  export const getInstrumentBySymbol = (state, symbol) =>
    state.instrument.instruments.find(inst => inst.Symbol === symbol);
  
  export default createSelector(
    [state => state.position.positions, selectedInstrumentSelector],
    (positions, instrument) => {
      let defaultPostions = {
        position1: {},
        position2: {}
      };
  
      if (
        !instrument ||
        !positions[instrument.Product1] ||
        !positions[instrument.Product2]
      ) {
        return defaultPostions;
      }
  
      const position1 = formatPosition(
        positions[instrument.Product1],
        instrument.Product1Symbol
      );
      const position2 = formatPosition(
        positions[instrument.Product2],
        instrument.Product2Symbol
      );
  
      return { position1, position2 };
    }
  );
  