import * as React from 'react';
import { createCrudList } from './createCrudList';
import { createCrudEditor } from './createCrudEditor';
var validateDefinition = function (item) {
    var err = [];
    if (!item.skipList && !item.getList) {
        err.push('Provide a getlist or skipList');
    }
    if (!item.skipEditor && !item.getItemById) {
        err.push('Provide (getItemById and saveItem) or skipEditor');
    }
    if (err.length !== 0) {
        throw Error(err.join(', '));
    }
};
export var createCrud = function (def) {
    validateDefinition(def);
    var ret = [];
    if (!def.skipList) {
        ret.push({
            path: "/" + def.path,
            component: function (p) { return createCrudList(def); }
        });
    }
    if (!def.skipEditor) {
        var Editor_1 = createCrudEditor(def);
        ret.push({
            path: "/" + def.path + "/:id",
            component: function (p) { return <Editor_1 id={p.match.params.id} onQuit={function () { return p.history.goBack(); }} onSaved={function (i) { return def.saveItem && def.saveItem(i).then(function (i) {
                p.history.replace("/" + def.path + "/" + i[def.key]);
            }); }}/>; }
        });
    }
    return ret;
};
