'use strict'

const _ = require('lodash')
const pluralize = require('pluralize')
const JSONAPISerializer = require('jsonapi-serializer').Serializer

const foreignKey = require('../sql-builder/foreign-key')
const {castTypes} = require('./cast-types')

function fetchBelongsToRelation ({model, belongsTo, query}) {
  const relationName = belongsTo.name
  const fkName = foreignKey(relationName)

  return Promise.all([ model.all(query), belongsTo.all() ])
    .then(([modelRows, relationRows]) => {
      const castModelRows = castTypes(modelRows, model.schema)
      const castRelationRows = castTypes(relationRows, belongsTo.schema)

      const dataSet = castModelRows.map((row) => {
        row[relationName] = _.find(castRelationRows, {'id': row[fkName]})
        return row
      })

      const options = { attributes: model.schema.dataFieldsNames.concat(relationName) }
      options[relationName] = { ref: 'id', attributes: belongsTo.schema.dataFieldsNames }
      const serializer = new JSONAPISerializer(model.name, options)

      return serializer.serialize(dataSet)
    })
}

function fetchHasManyRelation ({model, hasMany}) {
  const relationName = pluralize(hasMany.name)
  const fkName = foreignKey(model.name)

  return Promise.all([ model.all(), hasMany.all() ])
    .then(([modelRows, relationRows]) => {
      const castModelRows = castTypes(modelRows, model.schema)
      const castRelationRows = castTypes(relationRows, hasMany.schema)

      const dataSet = castModelRows.map((row) => {
        row[relationName] = _.filter(castRelationRows, [fkName, row.id])
        return row
      })

      const options = { attributes: model.schema.dataFieldsNames.concat(relationName) }
      options[relationName] = { ref: 'id', attributes: hasMany.schema.dataFieldsNames }
      const serializer = new JSONAPISerializer(model.name, options)

      return serializer.serialize(dataSet)
    })
}

module.exports = {
  fetchBelongsToRelation,
  fetchHasManyRelation
}

@@ -248,845 +248,8 @@
 ')%0A%0A
-function fetchBelongsToRelation (%7Bmodel, belongsTo, query%7D) %7B%0A  const relationName = belongsTo.name%0A  const fkName = foreignKey(relationName)%0A%0A  return Promise.all(%5B model.all(query), belongsTo.all() %5D)%0A    .then((%5BmodelRows, relationRows%5D) =%3E %7B%0A      const castModelRows = castTypes(modelRows, model.schema)%0A      const castRelationRows = castTypes(relationRows, belongsTo.schema)%0A%0A      const dataSet = castModelRows.map((row) =%3E %7B%0A        row%5BrelationName%5D = _.find(castRelationRows, %7B'id': row%5BfkName%5D%7D)%0A        return row%0A      %7D)%0A%0A      const options = %7B attributes: model.schema.dataFieldsNames.concat(relationName) %7D%0A      options%5BrelationName%5D = %7B ref: 'id', attributes: belongsTo.schema.dataFieldsNames %7D%0A      const serializer = new JSONAPISerializer(model.name, options)%0A%0A      return serializer.serialize(dataSet)%0A    %7D)%0A%7D%0A%0A
 func
@@ -1088,34 +1088,8 @@
 = %7B%0A
-  fetchBelongsToRelation,%0A
   fe

remove old fetchBelongsToRelation method