/* global ObjectId */

// Our general requirements
const mongoose = require('mongoose');
const path = require("path");
const config = require("config");
const JXPHelper = require('jxp-helper');


// Set some global types
global.ObjectId = mongoose.Schema.Types.ObjectId;
global.Mixed = mongoose.Schema.Types.Mixed;
// Add this to the top of your model to avoid eslint warnings: /* global ObjectId Mixed */

const model_dir = config.model_dir || path.join(__dirname, "../models");

const getModelFileFromRef = ref => {
    return path.join(model_dir, `${String(ref).toLowerCase()}_model`);
}

class Schema extends mongoose.Schema {
    constructor(definition, opts) {
        // Set default options
        opts = Object.assign({
            timestamps: true,
            toJSON: { virtuals: true },
            toObject: { virtuals: true }
        }, opts);
        // Set default defiitions
        definition = Object.assign({
            _deleted: { type: Boolean, default: false, index: true },
            _owner_id: { type: ObjectId, link: "User", map_to: "_owner" }
        }, definition);
        // construct our parent
        super(definition, opts);
        // Some properties
        this.opts = opts;
        this.definition = definition;
        // Action!
        this.setPerms();
        this.generateLinks();
        // Set up our jxp-helper so that we call call the API from within the API (if we've set config.apikey)
        const jxp_settings = {};
        if (config.apikey) jxp_settings.apikey = config.apikey;
        if (config.server) jxp_settings.server = config.server;
        if (opts.apikey) jxp_settings.apikey = opts.apikey;
        if (opts.server) jxp_settings.server = opts.server;
        if (jxp_settings.apikey && jxp_settings.server) this.jxphelper = new JXPHelper(jxp_settings);
    }

    setPerms() {
        // Create the perms
        this.set("_perms", Object.assign({
            admin: "", // CRUD = Create, Retrieve, Update and Delete
            owner: "",
            user: "",
            all: "" // Unauthenticated users will be able to read from test, but that is all
        }, this.opts.perms));
    }

    generateLinks() {
        // Find the links in our definitions and create virtuals for non-destructive populating
        // Example: link_id: { type: ObjectId, link: "link", }
        // Example with a custom key: link_id: { type: ObjectId, link: "link", map_to: "custom_name" }
        for (let key of Object.keys(this.definition)) {
            let def = this.definition[key];
            let is_array = false;
            if (def[0]) {
                def = def[0];
                is_array = true;
            }
            if (!def.link) continue;
            const virtual_name = def.map_to || def.virtual || String(def.link).toLowerCase();
            require(getModelFileFromRef(def.link));
            this.virtual(virtual_name, {
                ref: def.link,
                localField: key,
                foreignField: "_id",
                justOne: def.justOne || !is_array,
                options: Object.assign({}, def.options)
            })
        }
    }
}

// Now we don't even need to import mongoose into our models for type
Schema.Types = mongoose.Schema.Types;

// Slightly self-referential, try not to think about it
Schema.model = mongoose.model;

module.exports = Schema;
@@ -187,16 +187,352 @@
 lper');%0A
+// Set up our jxp-helper so that we call call the API from within the API (if we've set config.apikey)%0Aconst jxp_settings = %7B%7D;%0Aif (config.apikey) jxp_settings.apikey = config.apikey;%0Aif (config.server) jxp_settings.server = config.server;%0Aif (jxp_settings.apikey && jxp_settings.server) global.jxphelper = new JXPHelper(jxp_settings);%0A
 %0A%0A// Set
@@ -1660,502 +1660,8 @@
 ();%0A
-        // Set up our jxp-helper so that we call call the API from within the API (if we've set config.apikey)%0A        const jxp_settings = %7B%7D;%0A        if (config.apikey) jxp_settings.apikey = config.apikey;%0A        if (config.server) jxp_settings.server = config.server;%0A        if (opts.apikey) jxp_settings.apikey = opts.apikey;%0A        if (opts.server) jxp_settings.server = opts.server;%0A        if (jxp_settings.apikey && jxp_settings.server) this.jxphelper = new JXPHelper(jxp_settings);%0A
     

Make jxphelper global