import { prop, map, filter, pipe, apply } from './functional-util.js'

/**
 * @typedef {Object} HeaderObject
 * @property {string} link
 * @property {number} level
 * @property {string} text
 */

/**
 * @typedef {HeaderObject[]} HeaderList
 */

/**
 * @param {string} link
 * @param {number} level
 * @param {string} text
 * @returns {HeaderObject}
 */
export function createHeader (link, level, text) {
  return {
    link,
    level,
    text
  }
}

/**
 * @param {string} query
 * @returns {function(root: Element): NodeList}
 */
export function querySelectorAll (query) {
  return function querySelectorAllInner (root) {
    return root.querySelectorAll(query)
  }
}

/**
 * @param {string} query
 * @returns {function(root: Element): Element[]}
 */
export function querySelectorAllArray (query) {
  return pipe(querySelectorAll(query), Array.from)
}

/**
 * @function
 * @param {Node}
 * @returns {boolean}
 */
export const hasText = pipe(prop('textContent'), Boolean)

/**
 * @function
 * @param {Node[]}
 * @returns {Node[]}
 */
export const filterEmptyText = filter(hasText)

export function createHeaders (...fun) {
  return pipe(filterEmptyText, ...fun, map(apply(createHeader)))
}

/**
 * @function
 * @param {Element}
 * @returns {Element[]}
 */
export const selectAllHeaderElement = querySelectorAllArray('h1, h2, h3, h4, h5, h6')

import { prop, map, filter, pipe, apply } from './functional-util.js'

/**
 * @typedef {Object} HeaderObject
 * @property {string} link
 * @property {number} level
 * @property {string} text
 */

/**
 * @typedef {HeaderObject[]} HeaderList
 */

/**
 * @param {string} link
 * @param {number} level
 * @param {string} text
 * @returns {HeaderObject}
 */
export function createHeader (link, level, text) {
  return {
    link,
    level,
    text
  }
}

/**
 * @param {string} query
 * @returns {function(root: Element): NodeList}
 */
export function querySelectorAll (query) {
  return function querySelectorAllInner (root) {
    return root.querySelectorAll(query)
  }
}

/**
 * @param {string} query
 * @returns {function(root: Element): Element[]}
 */
export function querySelectorAllArray (query) {
  return pipe(querySelectorAll(query), Array.from)
}

/**
 * @function
 * @param {Node}
 * @returns {boolean}
 */
export const hasText = pipe(prop('textContent'), Boolean)

/**
 * @function
 * @param {Node[]}
 * @returns {Node[]}
 */
export const filterEmptyText = filter(hasText)

/**
 * @param {...function} fun
 * @returns {function(root: Element): HeaderList}
 */
export function createHeaders (...fun) {
  return pipe(filterEmptyText, ...fun, map(apply(createHeader)))
}

/**
 * @function
 * @param {Element}
 * @returns {Element[]}
 */
export const selectAllHeaderElement = querySelectorAllArray('h1, h2, h3, h4, h5, h6')

add missing JSDoc comment