"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
        s = arguments[i];
        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
            t[p] = s[p];
    }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable:jsx-no-multiline-js
// tslint:disable:jsx-no-lambda
var React = require("react");
var _ = require("lodash");
var react_redux_1 = require("react-redux");
var core_1 = require("@jsonforms/core");
var property_util_1 = require("../services/property.util");
var reducers_1 = require("../reducers");
var Button_1 = require("@material-ui/core/Button");
var DialogTitle_1 = require("@material-ui/core/DialogTitle");
var DialogContent_1 = require("@material-ui/core/DialogContent");
var DialogActions_1 = require("@material-ui/core/DialogActions");
var Dialog_1 = require("@material-ui/core/Dialog");
var List_1 = require("@material-ui/core/List");
var ListItem_1 = require("@material-ui/core/ListItem");
var ListItemText_1 = require("@material-ui/core/ListItemText");
var AddItemDialog = /** @class */ (function (_super) {
    __extends(AddItemDialog, _super);
    function AddItemDialog() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    AddItemDialog.prototype.render = function () {
        var _a = this.props, add = _a.add, path = _a.path, closeDialog = _a.closeDialog, dialogProps = _a.dialogProps, setSelection = _a.setSelection, rootData = _a.rootData, 
        /**
         * Self contained schemas of the corresponding schema
         */
        containerProperties = _a.containerProperties;
        return (React.createElement(Dialog_1.default, __assign({ id: 'dialog' }, dialogProps),
            React.createElement(DialogTitle_1.default, { id: 'dialog-title' }, "Select item to create"),
            React.createElement(DialogContent_1.default, null,
                React.createElement(List_1.default, null, containerProperties
                    .map(function (prop) {
                    return React.createElement(ListItem_1.default, { button: true, key: property_util_1.findPropertyLabel(prop) + "-button", onClick: function () {
                            var newData = _.keys(prop.schema.properties).reduce(function (d, key) {
                                if (prop.schema.properties[key].default) {
                                    d[key] = prop.schema.properties[key].default;
                                }
                                // FIXME generate id if identifying property
                                // is set in editor to allow id refs
                                return d;
                            }, {});
                            var arrayPath = core_1.Paths.compose(path, prop.property);
                            var array = core_1.Resolve.data(rootData, arrayPath);
                            var selectionIndex = _.isEmpty(array) ? 0 : array.length;
                            var selectionPath = core_1.Paths.compose(arrayPath, selectionIndex.toString());
                            add(path, prop, newData);
                            setSelection(prop.schema, newData, selectionPath)();
                            closeDialog();
                        } },
                        React.createElement(ListItemText_1.default, { primary: prop.label }));
                }))),
            React.createElement(DialogActions_1.default, null,
                React.createElement(Button_1.default, { onClick: closeDialog, variant: 'outlined', color: 'primary' }, "Close"))));
    };
    return AddItemDialog;
}(React.Component));
var mapStateToProps = function (state, ownProps) {
    var containerProps = reducers_1.getContainerProperties(state);
    var containerProperties;
    if (_.has(containerProps, ownProps.schema.$id)) {
        containerProperties = containerProps[ownProps.schema.$id];
    }
    else {
        containerProperties = property_util_1.retrieveContainerProperties(ownProps.schema, ownProps.schema);
    }
    return {
        rootData: core_1.getData(state),
        containerProperties: containerProperties,
        rootSchema: core_1.getSchema(state),
        path: ownProps.path,
        schema: ownProps.schema,
        closeDialog: ownProps.closeDialog,
        dialogProps: ownProps.dialogProps,
        setSelection: ownProps.setSelection
    };
};
var mapDispatchToProps = function (dispatch) { return ({
    add: function (path, prop, newData) {
        dispatch(core_1.update(core_1.Paths.compose(path, prop.property), function (array) {
            if (_.isEmpty(array)) {
                return [newData];
            }
            array.push(newData);
            return array;
        }));
    }
}); };
exports.default = react_redux_1.connect(mapStateToProps, mapDispatchToProps)(AddItemDialog);
//# sourceMappingURL=AddItemDialog.js.map