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

import { UIDatabase, createRecord } from '../database';

const COLUMN_WIDTH = 1;
const COLUMN_SORTABLE = false;
const COLUMN_EDITABLE = false;

const COLUMNS = {
  DESCRIPTION: {
    title: 'Description',
    key: 'description',
    type: 'string',
    width: COLUMN_WIDTH,
    sortable: COLUMN_SORTABLE,
    editable: COLUMN_EDITABLE,
  },
  CODE: {
    title: 'Code',
    key: 'code',
    type: 'string',
    width: COLUMN_WIDTH,
    sortable: COLUMN_SORTABLE,
    editable: COLUMN_EDITABLE,
  },
};

/**
 * Intialise a new indicator value.
 *
 * @param {IndicatorAttribute} row
 * @param {IndicatorAttribute} column
 * @param {Period} period
 */
export const initialiseRowColumnValue = (row, column, period) => {
  UIDatabase.write(() => {
    createRecord(UIDatabase, 'IndicatorValue', row, column, period);
  });
};

/**
 * Get value for indicator row, column tuple.
 *
 * @param {IndicatorAttribute} row
 * @param {IndicatorAttribute} column
 * @return {IndicatorValue}
 */
export const getIndicatorRowColumnValue = (row, column, period) => {
  const columnValues = column?.values?.filter(
    ({ period: valuePeriod }) => valuePeriod.ID === period.ID
  );
  const rowValues = row?.values?.filter(({ period: valuePeriod }) => valuePeriod.ID === period.ID);
  const columnValueIds = columnValues?.map(({ id }) => id);
  const rowValueIds = rowValues?.map(({ id }) => id);
  const [rowColumnValueId] = columnValueIds?.filter(id => rowValueIds.includes(id)) || [];
  const rowColumnValue = column?.values?.find(({ id }) => id === rowColumnValueId);

  if (!rowColumnValue) {
    initialiseRowColumnValue(row, column, period);
    return getIndicatorRowColumnValue(row, column, period);
  }

  return rowColumnValue;
};

/**
 * Get indicator data table columns.
 *
 * @param {ProgramIndicator} indicator
 * @param {Period} period
 * @returns {Array.<object>}
 */
// eslint-disable-next-line no-unused-vars
const getIndicatorTableColumns = (indicator, period) => {
  const descriptionColumn = COLUMNS.DESCRIPTION;
  const codeColumn = COLUMNS.CODE;
  // eslint-disable-next-line no-unused-vars
  const valueColumns = indicator.columns.map(({ description, code, valueType }) => ({
    title: description,
    key: description,
    type: valueType,
    width: COLUMN_WIDTH,
    sortable: COLUMN_SORTABLE,
    editable: COLUMN_EDITABLE,
  }));

  return [descriptionColumn, codeColumn, ...valueColumns];
};

/**
 * Get indicator data table rows.
 *
 * @param {ProgramIndicator} indicator
 * @param {Period} period
 * @returns {Array.<object>}
 */
const getIndicatorTableRows = (indicator, period) =>
  indicator.rows.map(row => {
    const { id, description, code } = row;
    const values = indicator.columns.reduce((acc, column) => {
      const { description: key } = column;
      const value = getIndicatorRowColumnValue(row, column, period);
      return { ...acc, [key]: value.value };
    }, []);
    return { id, description, code, ...values };
  });

/**
 * Get indicator data table rows and columns.
 *
 * @param {ProgramIndicator} indicator
 * @param {Period} period
 * @returns {object}
 */
const getIndicatorTableData = (indicator, period) => {
  const columns = getIndicatorTableColumns(indicator, period);
  const rows = getIndicatorTableRows(indicator, period);
  return { columns, rows };
};

export { getIndicatorTableData };

@@ -134,79 +134,90 @@
 UMN_
-WIDTH = 1;%0Aconst COLUMN_SORTABLE = false;%0Aconst COLUMN_EDITABLE =
+DEFAULTS = %7B%0A  type: 'string',%0A  width: 1,%0A  sortable: false,%0A  editable:
  false
+,%0A%7D
 ;%0A%0Ac
@@ -308,267 +308,101 @@
     
-type: 'string',%0A    width: COLUMN_WIDTH,%0A    sortable: COLUMN_SORTABLE,%0A    editable: COLUMN_EDITABLE,%0A  %7D,%0A  CODE: %7B%0A    title: 'Code',%0A    key: 'code',%0A    type: 'string',%0A    width: COLUMN_WIDTH,%0A    sortable: COLUMN_SORTABLE,%0A    editable: COLUMN_EDITABLE
+...COLUMN_DEFAULTS,%0A  %7D,%0A  CODE: %7B%0A    title: 'Code',%0A    key: 'code',%0A    ...COLUMN_DEFAULTS
 ,%0A  
@@ -863,16 +863,17 @@
  @return
+s
  %7BIndica
@@ -2162,89 +2162,26 @@
     
-width: COLUMN_WIDTH,%0A    sortable: COLUMN_SORTABLE,%0A    editable: COLUMN_EDITABLE
+...COLUMN_DEFAULTS
 ,%0A  

Tidy up getIndicatorTableData