/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2019
 */

import { createSelector } from 'reselect';
import { UIDatabase } from '../database';

export const selectHasItemsAndQuantity = ({ prescription }) => {
  const { transaction } = prescription;
  const { totalQuantity = 0, items = [] } = transaction || {};
  const hasItems = items.length > 0;
  const hasQuantity = totalQuantity > 0;
  return hasItems && hasQuantity;
};

export const selectPrescriptionPatient = ({ prescription }) => {
  const { transaction } = prescription;
  const { otherParty } = transaction || {};

  return otherParty;
};

export const selectPrescriptionPrescriber = ({ prescription }) => {
  const { transaction } = prescription;
  const { prescriber } = transaction || {};

  return prescriber;
};

export const selectPrescriberName = ({ prescription }) => {
  const currentPrescriber = selectPrescriptionPrescriber({ prescription });

  return `${currentPrescriber?.firstName} ${currentPrescriber?.lastName}`.trim();
};

export const selectPatientName = state => {
  const currentPatient = selectPrescriptionPatient(state);

  return `${currentPatient?.firstName} ${currentPatient?.lastName}`.trim();
};

export const selectItemSearchTerm = ({ prescription }) => {
  const { itemSearchTerm } = prescription;

  return itemSearchTerm;
};

export const selectItems = ({ prescription }) => {
  const { items } = prescription;

  return items;
};

export const selectFilteredAndSortedItems = createSelector(
  [selectItemSearchTerm, selectItems],
  (itemSearchTerm, items) => {
    // Filter the items by the entered search term.
    const filteredItems = items.filtered('name CONTAINS[c] $0', itemSearchTerm);

    // Keep the items sorted alphabetically.
    const sortedItems = filteredItems.sorted('name');

    // Split the items by quantity - showing out-of-stock items at the end of the list.
    const itemsWithStock = sortedItems.filtered('ANY batches.numberOfPacks > 0').slice();
    const itemsWithoutStock = sortedItems.filtered('ALL batches.numberOfPacks == 0').slice();

    return [...itemsWithStock, ...itemsWithoutStock];
  }
);

export const selectSelectedRows = ({ prescription }) => {
  const { transaction } = prescription;
  const { items = [] } = transaction || {};

  return items.reduce((acc, { item }) => ({ ...acc, [item.id]: true }), {});
};

export const selectTransactionCategoryName = ({ prescription }) => {
  const { transaction } = prescription;
  const { category } = transaction || {};
  const { name } = category || {};
  return name ?? '';
};

export const selectTransactionComment = ({ prescription }) => {
  const { transaction } = prescription;
  const { comment } = transaction || {};
  return comment ?? '';
};

export const selectPatientType = ({ prescription }) => {
  const { transaction } = prescription;
  const { user1 } = transaction || {};
  return user1 ?? '';
};

export const selectPrescriptionCategories = () =>
  UIDatabase.objects('PrescriptionCategory').map(({ name }) => name);

@@ -2151,19 +2151,20 @@
 lect
-SelectedRow
+NumberOfItem
 s = 
@@ -2229,133 +2229,335 @@
 tion
-;%0A  const %7B items = %5B%5D
+ %7C%7C %7B%7D;%0A  return transaction?.items.length %7C%7C 0;%0A%7D;%0A%0Aexport const selectPrescription = (%7B prescription
  %7D
+)
  =
- transaction %7C%7C %7B%7D;%0A%0A  return items.reduce((acc, %7B item %7D) =%3E (%7B ...acc, %5Bitem.id%5D: true %7D), %7B%7D);%0A%7D
+%3E prescription.transaction;%0A%0Aexport const selectPrescriptionItems = createSelector(%0A  %5BselectPrescription, selectNumberOfItems%5D,%0A  prescription =%3E %7B%0A    const %7B items %7D = prescription ?? %7B%7D;%0A    return items %7C%7C %5B%5D;%0A  %7D%0A)
 ;%0A%0Ae

Add additional selectors for prescription