import { createCrudFormEditor } from './lib/CrudEditor';
import * as React from 'react';
import { createCrudList } from './lib/CrudList';
var validateDefinition = function (cfg) {
    var err = [];
    if (!cfg.skipList && !cfg.listItems) {
        err.push('Provide a listItems or skipList');
    }
    if (!cfg.skipForm && !cfg.getItemById) {
        err.push('Provide (getItemById and saveItem) or skipForm');
    }
    if (err.length !== 0) {
        throw Error(err.join(', '));
    }
};
export var createCrud = function (cfg) {
    validateDefinition(cfg);
    var ret = [];
    if (!cfg.skipList) {
        var ListControl_1 = createCrudList(cfg);
        ret.push({
            path: "/" + cfg.path,
            component: function (p) { return <ListControl_1 />; }
        });
    }
    if (!cfg.skipForm) {
        var Editor_1 = createCrudFormEditor(cfg);
        ret.push({
            path: "/" + cfg.path + "/:id",
            component: function (p) { return <Editor_1 id={p.match.params.id} didQuit={function () { return p.history.goBack(); }} didSave={function (id) { return p.history.replace("/" + cfg.path + "/" + id); }}/>; }
        });
    }
    return ret;
};
