Source: utils.js

/* global lunr */
import lunr from 'lunr'
import mongoose from 'mongoose'
// const lunr = require('lunr')
// console.debug('<>><><><<>><><><><><><><', lunr)
/**
 * @author Eduardo Perotta de Almeida <web2solucoes@gmail.com>
 * @module utils
 * */

/**
 * createMethodSignature
 * Create default signature method object
 * @function
 * @param {string|object} error - The string or error object if have any
 * @param {object|array|number|string|boolean} data - Information about method execution
 * @return  {object} signature - Default methods signature format { error, data }
 * @return  {string|object} signature.error - Execution error
 * @return  {object|array|number|string|boolean} signature.data - Execution data
 */
export const createMethodSignature = (error = null, data = null) => {
  return { error, data }
}

/**
 * uuid
 * generates a Universally unique identifier string
 * @function
 * @return  {string} guid / uuid
 */
export function uuid () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0
    const v = c === 'x' ? r : (r & 0x3) | 0x8
    return v.toString(16)
  })
}

/**
 * toJSON -  stringify and parse an object<br> It uses native JSON internally.
 * @function
 * @param {string|object} obj - Valid JSON object or string
 * @return  {object} new JSON object
 */
export function toJSON (obj = '') {
  if (typeof obj === 'string') {
    return JSON.parse(obj)
  }
  return JSON.parse(JSON.stringify(obj))
}

/**
 * mongooseToDexieTableString
 * convert given Mongoose schema to a Dexie Table columns configuration. <br>
 * All columns inside returned configuration are indexed at IndexedDB
 * prepend __id as local primary key and _id for remote primary key
 * Local primary key is integer and auto incremented
 * @function
 * @return  {string} Dexie table configuration string
 */
export function mongooseToDexieTableString (schema) {
  const cols = []
  for (let propertyName in schema.paths) {
    if (Object.prototype.hasOwnProperty.call(schema.paths, propertyName)) {
      const property = schema.paths[propertyName]
      console.debug(property)
      const {
        instance, // instance is type
        _index, // ,
        options, // { default, index, required }
        // isRequired
      } = property
      // console.debug(propertyName, property)
      if (propertyName === '_id' || propertyName === '__id') {
        continue
      }
      if (!_index) {
        continue
      }
      if (instance === 'Array') {
        propertyName = `*${propertyName}`
        // * is MultiEntry Index on Dexie
      }
      // if unique
      cols.push(propertyName)
    }
  }
  return `++__id,_id${cols.length > 0 ? (',' + cols.join(',')) : ''}`
}

/**
 * getSearchTokenStream
 * generates a lunr search token. See {@link https://lunrjs.com/guides/searching.html|lunr search}
 * @function
 * @return {array} token
 */
export function getSearchTokenStream(text = '') {
  // console.log('xxxxxxxxx')
  // console.log('xxxxxxxxx', index)
  // const index = lunr()
  // return index.pipeline.run(lunr.tokenizer(text))
  const token = (lunr.tokenizer(text)).map(t => (t.str))
  return token
  // return lunr.tokenizer(text)
}

export const Schema = mongoose.Schema