import * as R from 'ramda';

const parseHighlightChunks = (indices = [], str) =>
  R.compose(
    R.cond([
      [
        R.isEmpty,
        R.always([
          {
            string: str,
            highlight: false,
          },
        ]),
      ],
      [R.T, (d) => d],
    ]),
    R.filter((chunk) => chunk && chunk.string),
    R.unnest,
    R.addIndex(R.map)((index, i) => {
      const previousIndex = i > 0 ? indices[i - 1] : 0;

      return [
        {
          string: str.slice(previousIndex[1] + 1, index[0]),
          highlight: false,
        },
        {
          string: str.slice(index[0], index[1] + 1),
          highlight: true,
        },
        i === indices.length - 1 && {
          string: str.slice(index[1] + 1),
          highlight: false,
        },
      ];
    }),
  )(indices);

export default parseHighlightChunks;
