import Config from '../../core/config'
import type { RangeTuple } from '../../types'

export default function convertMaskToIndices(
  matchmask: number[] = [],
  minMatchCharLength: number = Config.minMatchCharLength
): RangeTuple[] {
  const indices: RangeTuple[] = []
  let start = -1
  let end = -1
  let i = 0

  for (let len = matchmask.length; i < len; i += 1) {
    const match = matchmask[i]
    if (match && start === -1) {
      start = i
    } else if (!match && start !== -1) {
      end = i - 1
      if (end - start + 1 >= minMatchCharLength) {
        indices.push([start, end])
      }
      start = -1
    }
  }

  // (i-1 - start) + 1 => i - start
  if (matchmask[i - 1] && i - start >= minMatchCharLength) {
    indices.push([start, i - 1])
  }

  return indices
}
