"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        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 extendStatics(d, b);
    }
    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) || function () {
    __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;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable:jsx-no-multiline-js
// tslint:disable:jsx-no-lambda
var DialogTitle_1 = require("@material-ui/core/DialogTitle");
var React = require("react");
var react_redux_1 = require("react-redux");
var core_1 = require("@jsonforms/core");
var property_util_1 = require("../services/property.util");
var Button_1 = require("@material-ui/core/Button");
var _ = require("lodash");
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 core_2 = require("@material-ui/core");
var image_provider_util_1 = require("../helpers/image-provider.util");
var createData = function (defaultData, prop) {
    var foundData = _.find(defaultData, function (d) {
        return d.schemaPath === prop.schemaPath;
    });
    // default behavior is to read default property from schemas
    var predefined = _.keys(prop.schema.properties).reduce(function (acc, key) {
        if (prop.schema.properties[key].default) {
            acc[key] = prop.schema.properties[key].default;
        }
        // FIXME generate id if identifying property
        // is set in editor to allow id refs
        return acc;
    }, {});
    return _.merge(foundData ? foundData.data : {}, predefined);
};
var createPropLabel = function (prop, labelProvider) {
    var label = prop.label;
    if (labelProvider) {
        label = labelProvider(prop.schema, prop.schemaPath);
    }
    return prop.property + " [" + label + "]";
};
var AddItemDialog = /** @class */ (function (_super) {
    __extends(AddItemDialog, _super);
    function AddItemDialog() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.onClick = function (prop) {
            var _a = _this.props, add = _a.add, closeDialog = _a.closeDialog, defaultData = _a.defaultData, path = _a.path, rootData = _a.rootData, setSelection = _a.setSelection;
            var newData = createData(defaultData, prop);
            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();
        };
        return _this;
    }
    AddItemDialog.prototype.render = function () {
        var _this = this;
        var _a = this.props, closeDialog = _a.closeDialog, dialogProps = _a.dialogProps, 
        /**
         * Self contained schemas of the corresponding schema
         */
        containerProperties = _a.containerProperties, imageProvider = _a.imageProvider, labelProvider = _a.labelProvider;
        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, index) {
                    var label = createPropLabel(prop, labelProvider);
                    return (React.createElement(ListItem_1.default, { button: true, key: property_util_1.findPropertyLabel(prop) + "-button-" + index, onClick: function () { return _this.onClick(prop); } },
                        React.createElement(core_2.ListItemIcon, null, image_provider_util_1.wrapImageIfNecessary(imageProvider(prop.schema))),
                        React.createElement(ListItemText_1.default, { primary: label })));
                }))),
            React.createElement(DialogActions_1.default, null,
                React.createElement(Button_1.default, { onClick: closeDialog, variant: 'outlined', color: 'primary' }, "Close"))));
    };
    return AddItemDialog;
}(React.Component));
// TODO
var mapStateToProps = function (state, ownProps) {
    var containerProperties = property_util_1.findContainerProperties(ownProps.schema, core_1.getSchema(state), false);
    return {
        rootData: core_1.getData(state),
        defaultData: core_1.getDefaultData(state),
        containerProperties: containerProperties,
        rootSchema: core_1.getSchema(state),
        path: ownProps.path,
        schema: ownProps.schema,
        closeDialog: ownProps.closeDialog,
        dialogProps: ownProps.dialogProps,
        setSelection: ownProps.setSelection,
        labelProvider: ownProps.labelProvider
    };
};
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