import assert from 'assert';
import fp from 'mostly-func';

export default function prefixSelect(target, opts = { excepts: [] }) {
  assert(target, 'select target not provided.');

  return hook => {
    hook.params.query = hook.params.query || {};

    let excepts = [target, '*'].concat(opts.excepts || []);
    const schemas = hook.service.Model.schema && hook.service.Model.schema.paths;
    if (schemas) {
      excepts = excepts.concat(Object.keys(schemas));
    }
    let select = hook.params.query.$select || [];
    if (select.length) {
      if (fp.is(String, select)) {
        // convert string $select to array
        select = fp.map(fp.trim, fp.split(',', select));
      }
      select = select.map(field => {
        const excepted = excepts.some(except => field.startsWith(except));
        return excepted? field : target + '.'  + field;
      });
    }

    hook.params.query.$select = fp.uniq([target, '*'].concat(select));

    return hook;
  };
}

@@ -53,16 +53,130 @@
 func';%0A%0A
+/**%0A * prefix the $select with target excepts provided fields,%0A * used for populating with absolute type path%0A */%0A
 export d

comment prefixSelect hook func