/**
 * Compare model names by reference names.
 * More referenced names come first.
 * @function compareByRefs
 * @param {string[]} names - Model name
 * @param {object[]} refs - Reference name map
 * @returns {function} Compare function
 */
'use strict'

const { pascalcase } = require('stringcase')

/** @lends compareByRefs */
function compareByRefs (refs) {
  return function compare (a, b) {
    let aRefs = refs[ a ] || refs[ pascalcase(a) ]
    let bRefs = refs[ b ] || refs[ pascalcase(b) ]
    let shouldB = aRefs.indexOf(b) > -1
    if (shouldB) {
      return 1
    }
    let shouldA = bRefs.indexOf(a) > -1
    if (shouldA) {
      return -1
    }
    return aRefs.length - bRefs.length
  }
}

module.exports = compareByRefs
