import assert from 'assert';
import { Service, createService } from 'mostly-feathers-mongoose';
import fp from 'mostly-func';

import FolderModel from '../../models/folder.model';
import defaultHooks from './folder.hooks';

const defaultOptions = {
  name: 'folders'
};

export class FolderService extends Service {
  constructor (options) {
    options = fp.assignAll(defaultOptions, options);
    super(options);
  }

  setup (app) {
    super.setup(app);
    this.hooks(defaultHooks(this.options));

    // root folder
    this.get(null, { query: {
      path: '/'
    }}).then(result => {
      if (!result) {
        return this.create({
          title: 'Root',
          color: '#000000',
          path: '/'
        });
      }
    }).catch(console.error);

    // workspaces folder
    this.get(null, { query: {
      path: '/workspaces'
    }}).then(result => {
      if (!result) {
        return this.create({
          title: 'Workspaces',
          color: '#555555',
          path: '/workspaces'
        });
      }
    }).catch(console.error);
  }
}

export default function init (app, options, hooks) {
  options = { ModelName: 'folder', ...options };
  return createService(app, FolderService, FolderModel, options);
}

init.Service = FolderService;

@@ -33,17 +33,57 @@
 ort 
-%7B Service
+makeDebug from 'debug';%0Aimport %7B Service, helpers
 , cr
@@ -257,16 +257,78 @@
 ooks';%0A%0A
+const debug = makeDebug('playing:content-services:folders');%0A%0A
 const de
@@ -1155,19 +1155,780 @@
 error);%0A
-
   %7D
+%0A%0A  /*%0A   * move positions of documents in the folder%0A   */%0A  async move (id, data, params) %7B%0A    assert(data.selects, 'data.selects is not provided.');%0A    assert(data.target, 'data.target is not provided.');%0A    data.selects = fp.asArray(data.selects);%0A%0A    debug('move folder documents', id, data.selects, data.target);%0A    const svcDocuments = this.app.service('documents');%0A    const %5Btarget, selects%5D = await Promise.all(%5B%0A      svcDocuments.get(data.target).%0A      svcDocuments.find(%7B id: %7B $in: data.selects %7D %7D)%0A    %5D);%0A    assert(target, 'target item is not exists');%0A    assert(selects && selects.length, 'selects item is not exists');%0A%0A    return helpers.reorderPosition(this.Model, selects, target.position, %7B%0A      classify: 'parent'%0A    %7D);%0A  %7D  
 %0A%7D%0A%0Aexpo

add move action to folder service