"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 __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable:jsx-no-multiline-js
// tslint:disable:max-line-length
var _ = require("lodash");
var React = require("react");
var core_1 = require("@jsonforms/core");
var react_1 = require("@jsonforms/react");
var react_dnd_html5_backend_1 = require("react-dnd-html5-backend");
var react_dnd_1 = require("react-dnd");
var react_redux_1 = require("react-redux");
var recompose_1 = require("recompose");
var ObjectListItem_1 = require("./ObjectListItem");
var ExpandRootArray_1 = require("./ExpandRootArray");
var AddItemDialog_1 = require("./AddItemDialog");
var styles_1 = require("@material-ui/core/styles");
var Master = function (_a) {
    var schema = _a.schema, path = _a.path, selection = _a.selection, handlers = _a.handlers, uischema = _a.uischema, rootData = _a.rootData, filterPredicate = _a.filterPredicate, labelProviders = _a.labelProviders, imageProvider = _a.imageProvider;
    if (schema.items !== undefined) {
        return (React.createElement("ul", null,
            React.createElement(ExpandRootArray_1.ExpandRootArray, { rootData: rootData, schema: schema.items, path: path, selection: selection, handlers: handlers, uischema: uischema, filterPredicate: filterPredicate, labelProvider: labelProviders.forData, imageProvider: imageProvider })));
    }
    return (React.createElement("ul", null,
        React.createElement(ObjectListItem_1.default, { path: path, schema: schema, uischema: uischema, selection: selection, handlers: handlers, isRoot: true, filterPredicate: filterPredicate, labelProvider: labelProviders.forData, imageProvider: imageProvider })));
};
var isNotTuple = function (schema) { return !Array.isArray(schema.items); };
var styles = function () { return ({
    treeMasterDetailContent: {
        paddingTop: '1em',
        paddingBottom: '1em'
    },
    // tslint:disable-next-line: object-literal-key-quotes
    treeMasterDetail: {
        display: 'flex',
        flexDirection: 'column',
        // tslint:disable-next-line:object-literal-key-quotes
        '& $treeMasterDetailContent': {
            display: 'flex',
            flexDirection: 'row'
        }
    },
    // tslint:disable-next-line: object-literal-key-quotes
    treeMasterDetailMaster: {
        flex: 1,
        padding: '0.5em',
        height: 'auto',
        borderRight: '0.2em solid lightgrey',
        borderWidth: 'thin',
        // tslint:disable-next-line:object-literal-key-quotes
        '& ul': {
            listStyleType: 'none',
            margin: 0,
            padding: 0,
            position: 'relative',
            overflow: 'hidden',
            // tslint:disable-next-line:object-literal-key-quotes
            '&:after': {
                content: '""',
                position: 'absolute',
                left: '0.2em',
                height: '0.6em',
                bottom: '0'
            },
            '&:last-child::after': {
                display: 'none'
            }
        }
    },
    // tslint:disable-next-line: object-literal-key-quotes
    treeMasterDetailDetail: {
        flex: 3,
        padding: '0.5em',
        paddingLeft: '1em',
        // tslint:disable-next-line:object-literal-key-quotes
        '&:first-child': {
            marginRight: '0.25em'
        }
    }
}); };
var TreeWithDetailRenderer = /** @class */ (function (_super) {
    __extends(TreeWithDetailRenderer, _super);
    function TreeWithDetailRenderer() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.setSelection = function (schema, data, path) { return function () {
            _this.setState({
                selected: {
                    schema: schema,
                    data: data,
                    path: path
                }
            });
        }; };
        _this.openDialog = function (schema, path) { return function () {
            _this.setState({
                dialog: {
                    open: true,
                    schema: schema,
                    path: path
                }
            });
        }; };
        _this.closeDialog = function () {
            _this.setState({
                dialog: {
                    open: false,
                    schema: undefined,
                    path: undefined
                }
            });
        };
        return _this;
    }
    TreeWithDetailRenderer.prototype.componentWillMount = function () {
        var _a = this.props, uischema = _a.uischema, resolvedRootData = _a.resolvedRootData, scopedSchema = _a.scopedSchema;
        var controlElement = uischema;
        this.setState({
            dialog: {
                open: false,
                schema: undefined,
                path: undefined
            }
        });
        var path = core_1.Paths.fromScopable(controlElement);
        if (_.isArray(resolvedRootData)) {
            this.setState({
                selected: {
                    schema: scopedSchema.items,
                    data: resolvedRootData[0],
                    path: core_1.Paths.compose(path, '0')
                }
            });
        }
        else {
            this.setState({
                selected: {
                    schema: scopedSchema,
                    data: resolvedRootData,
                    path: path
                }
            });
        }
    };
    TreeWithDetailRenderer.prototype.render = function () {
        var _a = this.props, uischema = _a.uischema, schema = _a.schema, scopedSchema = _a.scopedSchema, visible = _a.visible, path = _a.path, resolvedRootData = _a.resolvedRootData, rootData = _a.rootData, addToRoot = _a.addToRoot, filterPredicate = _a.filterPredicate, labelProviders = _a.labelProviders, imageProvider = _a.imageProvider, classes = _a.classes;
        var controlElement = uischema;
        var dialogProps = {
            open: this.state.dialog.open
        };
        var resetSelection;
        if (scopedSchema.items !== undefined) {
            resetSelection = this.setSelection(scopedSchema.items, resolvedRootData[0], core_1.Paths.compose(path, '0'));
        }
        else {
            resetSelection = this.setSelection(scopedSchema, resolvedRootData, path);
        }
        var handlers = {
            onSelect: this.setSelection,
            onAdd: this.openDialog,
            resetSelection: resetSelection
        };
        var detailUiSchema = this.props.findUISchema(this.state.selected.schema, undefined, path);
        return (React.createElement("div", { hidden: !visible, className: classes.treeMasterDetail },
            React.createElement("div", null,
                React.createElement("label", null, typeof controlElement.label === 'string' ? controlElement.label : ''),
                Array.isArray(resolvedRootData) &&
                    React.createElement("button", { onClick: addToRoot(schema, path) }, "Add to root")),
            React.createElement("div", { className: classes.treeMasterDetailContent },
                React.createElement("div", { className: classes.treeMasterDetailMaster },
                    React.createElement(Master, { uischema: uischema, schema: scopedSchema, path: path, handlers: handlers, selection: this.state.selected.data, rootData: rootData, filterPredicate: filterPredicate, labelProviders: labelProviders, imageProvider: imageProvider })),
                React.createElement("div", { className: classes.treeMasterDetailDetail }, this.state.selected ?
                    React.createElement(react_1.JsonForms, { schema: this.state.selected.schema, path: this.state.selected.path, uischema: detailUiSchema }) : 'Select an item')),
            React.createElement("div", null, this.state.dialog.open &&
                React.createElement(AddItemDialog_1.default, { path: this.state.dialog.path, schema: this.state.dialog.schema, closeDialog: this.closeDialog, dialogProps: dialogProps, setSelection: this.setSelection, labelProvider: labelProviders.forSchema, imageProvider: imageProvider }))));
    };
    return TreeWithDetailRenderer;
}(react_1.Control));
exports.TreeWithDetailRenderer = TreeWithDetailRenderer;
var mapStateToProps = function (state, ownProps) {
    var path = core_1.Paths.compose(ownProps.path, core_1.Paths.fromScopable(ownProps.uischema));
    var visible = _.has(ownProps, 'visible') ? ownProps.visible : core_1.Runtime.isVisible(ownProps, state);
    var enabled = _.has(ownProps, 'enabled') ? ownProps.enabled : core_1.Runtime.isEnabled(ownProps, state);
    var rootData = core_1.getData(state);
    return {
        rootData: core_1.getData(state),
        label: _.get(ownProps.uischema, 'label'),
        resolvedRootData: core_1.Resolve.data(rootData, path),
        uischema: ownProps.uischema,
        schema: ownProps.schema,
        scopedSchema: core_1.Resolve.schema(ownProps.schema, ownProps.uischema.scope),
        findUISchema: core_1.findUISchema(state),
        path: path,
        visible: visible,
        enabled: enabled,
        filterPredicate: ownProps.filterPredicate,
        imageProvider: ownProps.imageProvider,
        labelProviders: ownProps.labelProviders
    };
};
var mapDispatchToProps = function (dispatch) { return ({
    addToRoot: function (schema, path) {
        return function () {
            if (isNotTuple(schema)) {
                dispatch(core_1.Actions.update(path, function (data) {
                    var clone = data.slice();
                    clone.push({});
                    return clone;
                }));
            }
        };
    }
}); };
var DnDTreeMasterDetail = recompose_1.compose(styles_1.withStyles(styles, { name: 'TreeMasterDetail' }), react_dnd_1.DragDropContext(react_dnd_html5_backend_1.default))(TreeWithDetailRenderer);
var ConnectedTreeWithDetailRenderer = react_redux_1.connect(mapStateToProps, mapDispatchToProps)(DnDTreeMasterDetail);
exports.default = ConnectedTreeWithDetailRenderer;
//# sourceMappingURL=TreeWithDetailRenderer.js.map