"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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
  The MIT License

  Copyright (c) 2017-2019 EclipseSource Munich
  https://github.com/eclipsesource/jsonforms

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
var core_1 = require("@jsonforms/core");
var react_1 = require("@jsonforms/react");
var core_2 = require("@material-ui/core");
var Delete_1 = __importDefault(require("@material-ui/icons/Delete"));
var find_1 = __importDefault(require("lodash/find"));
var map_1 = __importDefault(require("lodash/map"));
var range_1 = __importDefault(require("lodash/range"));
var react_2 = __importDefault(require("react"));
var react_redux_1 = require("react-redux");
var ArrayToolbar_1 = require("../layouts/ArrayToolbar");
var MaterialListWithDetailRenderer = /** @class */ (function (_super) {
    __extends(MaterialListWithDetailRenderer, _super);
    function MaterialListWithDetailRenderer() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            selectedIndex: undefined
        };
        _this.addItem = function (path, value) { return _this.props.addItem(path, value); };
        _this.removeItem = function (path, value) { return function () {
            _this.props.removeItems(path, [value])();
            if (_this.state.selectedIndex === value) {
                _this.state.selectedIndex = undefined;
            }
            else if (_this.state.selectedIndex > value) {
                _this.state.selectedIndex--;
            }
        }; };
        _this.handleListItemClick = function (index) { return function () {
            _this.setState({ selectedIndex: index });
        }; };
        _this.createDefaultValue = function () { return core_1.createDefaultValue(_this.props.schema); };
        return _this;
    }
    MaterialListWithDetailRenderer.prototype.render = function () {
        var _this = this;
        var _a = this.props, required = _a.required, visible = _a.visible, errors = _a.errors, path = _a.path, data = _a.data, schema = _a.schema, label = _a.label;
        return (react_2.default.createElement(core_2.Hidden, { xsUp: !visible },
            react_2.default.createElement(ArrayToolbar_1.ArrayLayoutToolbar, { label: core_1.computeLabel(core_1.isPlainLabel(label) ? label : label.default, required), errors: errors, path: path, addItem: this.addItem, createDefault: this.createDefaultValue }),
            react_2.default.createElement(core_2.Grid, { container: true, direction: 'row', spacing: 16 },
                react_2.default.createElement(core_2.Grid, { item: true, xs: 3 },
                    react_2.default.createElement(core_2.List, null, data > 0 ? (map_1.default(range_1.default(data), function (index) { return (react_2.default.createElement(ConnectedListWithDetailMasterItem, { index: index, path: path, schema: schema, handleSelect: _this.handleListItemClick, removeItem: _this.removeItem, selected: _this.state.selectedIndex === index, key: index })); })) : (react_2.default.createElement("p", null, "No data")))),
                react_2.default.createElement(core_2.Grid, { item: true, xs: true }, this.renderDetail()))));
    };
    MaterialListWithDetailRenderer.prototype.renderDetail = function () {
        var _a = this.props, uischemas = _a.uischemas, schema = _a.schema, uischema = _a.uischema, path = _a.path;
        if (this.state.selectedIndex !== undefined) {
            var foundUISchema = core_1.findUISchema(uischemas, schema, uischema.scope, path, undefined, uischema);
            return (react_2.default.createElement(react_1.ResolvedJsonForms, { uischema: foundUISchema, schema: schema, path: core_1.composePaths(path, "" + this.state.selectedIndex) }));
        }
        return react_2.default.createElement(core_2.Typography, { variant: 'h6' }, "No Selection");
    };
    return MaterialListWithDetailRenderer;
}(react_2.default.Component));
exports.MaterialListWithDetailRenderer = MaterialListWithDetailRenderer;
var ConnectedMaterialListWithDetailRenderer = react_redux_1.connect(core_1.mapStateToArrayLayoutProps, core_1.mapDispatchToArrayControlProps)(MaterialListWithDetailRenderer);
exports.default = ConnectedMaterialListWithDetailRenderer;
ConnectedMaterialListWithDetailRenderer.displayName =
    'MaterialListWithDetailRenderer';
exports.materialListWithDetailTester = core_1.rankWith(4, core_1.and(core_1.uiTypeIs('ListWithDetail'), core_1.isObjectArray));
var ListWithDetailMasterItem = /** @class */ (function (_super) {
    __extends(ListWithDetailMasterItem, _super);
    function ListWithDetailMasterItem() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ListWithDetailMasterItem.prototype.render = function () {
        var _a = this.props, selected = _a.selected, handleSelect = _a.handleSelect, index = _a.index, childLabel = _a.childLabel, removeItem = _a.removeItem, path = _a.path;
        return (react_2.default.createElement(core_2.ListItem, { button: true, selected: selected, onClick: handleSelect(index) },
            react_2.default.createElement(core_2.ListItemAvatar, null,
                react_2.default.createElement(core_2.Avatar, { "aria-label": 'Index' }, index + 1)),
            react_2.default.createElement(core_2.ListItemText, { primary: childLabel }),
            react_2.default.createElement(core_2.ListItemSecondaryAction, null,
                react_2.default.createElement(core_2.IconButton, { "aria-label": 'Delete', onClick: removeItem(path, index) },
                    react_2.default.createElement(Delete_1.default, null)))));
    };
    return ListWithDetailMasterItem;
}(react_2.default.Component));
/**
 * Map state to control props.
 * @param state the store's state
 * @param ownProps any own props
 * @returns {StatePropsOfControl} state props for a control
 */
exports.mapStateToMasterListItemProps = function (state, ownProps) {
    var schema = ownProps.schema, path = ownProps.path, index = ownProps.index;
    var firstPrimitiveProp = schema.properties
        ? find_1.default(Object.keys(schema.properties), function (propName) {
            var prop = schema.properties[propName];
            return (prop.type === 'string' ||
                prop.type === 'number' ||
                prop.type === 'integer');
        })
        : undefined;
    var childPath = core_1.composePaths(path, "" + index);
    var childData = core_1.Resolve.data(core_1.getData(state), childPath);
    var childLabel = firstPrimitiveProp ? childData[firstPrimitiveProp] : '';
    return __assign({}, ownProps, { childLabel: childLabel });
};
var ConnectedListWithDetailMasterItem = react_redux_1.connect(exports.mapStateToMasterListItemProps)(ListWithDetailMasterItem);
//# sourceMappingURL=MaterialListWithDetailRenderer.js.map