/*
 *  find.js
 *
 *  David Janes
 *  IOTDB.org
 *  2017-08-05
 *
 *  Copyright [2013-2018] [David P. Janes]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

"use strict";

const _ = require("iotdb-helpers");

const assert = require("assert");

const util = require("./util");

/**
 */
const find = (_self, done) => {
    const self = _.d.clone.shallow(_self)
    const method = "find";

    assert.ok(self.mongo_collection, `${method}: expected self.mongo_collection`)
    assert.ok(_.is.JSON(self.query) || _.is.Nullish(self.query), `${method}: expected self.query to be a JSON-like object or Null`)

    const query = util.restore_ids(self.query || {});
    const find_in = self.find_in || exports.find.DEFAULT;

    const options = Object.assign({
        skip: 0
    }, find_in)

    if (self.pager) {
        options.skip = _.coerce.to.Integer(self.pager, 0)
    }
    if (self.query_limit) {
        options.limit = self.query_limit;
    }
    if (self.projection) {
        if (_.is.Array(self.projection)) {
            options.projection = {}
            self.projection.forEach(key => options.projection[key] = 1)
        } else {
            options.projection = _.d.clone(self.projection)
        }
    }

    self.mongo_collection.find(query, options).toArray((error, mongo_result) => {
        if (error) {
            return done(util.intercept(self)(error))
        }

        self.jsons = util.safe_ids(mongo_result);
        self.json = self.jsons.length ? self.jsons[0] : null;

        self.mongo_result = mongo_result;

        done(null, self)
    })
}

/**
 *  API
 */
exports.find = _.promise.denodeify(find)

exports.find.DEFAULT = {}

@@ -1949,17 +1949,16 @@
 _result)
-;
 %0A       
@@ -2010,17 +2010,16 @@
 %5D : null
-;
 %0A%0A      
@@ -2052,17 +2052,1691 @@
 o_result
-;
+%0A%0A        self.cursor = null%0A%0A        if (options.limit) %7B%0A            self.cursor = %7B%0A                first: %220%22,%0A                previous: null,%0A                current: %60$%7Boptions.skip%7D%60,%0A                next: null,%0A%0A                is_first: null,%0A                is_last: null,%0A                has_next: null,%0A                has_previous: null,%0A                is_previous_first: null,%0A%0A                implements_previous: true,%0A%0A                n_results: self.jsons.length,%0A                n_start: options.skip,%0A            %7D%0A%0A            if (self.jsons.length === 0) %7B%0A                self.cursor.is_first = true%0A                self.cursor.is_last = true%0A            %7D else %7B%0A                self.cursor.next = %60$%7BMath.min(options.limit, self.jsons.length) + options.skip%7D%60%0A%0A                if (self.jsons.length %3C options.limit) %7B%0A                    self.cursor.next = null%0A                    self.cursor.is_last = true%0A                    self.cursor.has_next = false%0A                %7D else %7B%0A                    self.cursor.has_next = true%0A                %7D%0A%0A                if (options.skip === 0) %7B%0A                    self.cursor.is_first = true%0A                %7D%0A%0A                const previous = options.skip - options.limit%0A                self.cursor.previous = %60$%7BMath.max(0, previous)%7D%60%0A                if (previous === 0) %7B%0A                    self.cursor.has_previous = true%0A                    self.cursor.is_previous_first = true%0A                %7D else if (previous %3C 0) %7B%0A                    self.cursor.has_previous = false%0A                %7D else %7B%0A                    self.cursor.has_previous = true%0A                %7D%0A%0A            %7D%0A        %7D
 %0A%0A      

add paging