"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 ExpansionPanelDetails_1 = __importDefault(require("@material-ui/core/ExpansionPanelDetails"));
var find_1 = __importDefault(require("lodash/find"));
var range_1 = __importDefault(require("lodash/range"));
var react_1 = __importDefault(require("react"));
var core_1 = require("@jsonforms/core");
var react_2 = require("@jsonforms/react");
var IconButton_1 = __importDefault(require("@material-ui/core/IconButton"));
var ExpansionPanel_1 = __importDefault(require("@material-ui/core/ExpansionPanel"));
var map_1 = __importDefault(require("lodash/map"));
var ExpansionPanelSummary_1 = __importDefault(require("@material-ui/core/ExpansionPanelSummary"));
var core_2 = require("@material-ui/core");
var ExpandMore_1 = __importDefault(require("@material-ui/icons/ExpandMore"));
var Avatar_1 = __importDefault(require("@material-ui/core/Avatar"));
var Paper_1 = __importDefault(require("@material-ui/core/Paper"));
var Delete_1 = __importDefault(require("@material-ui/icons/Delete"));
var react_redux_1 = require("react-redux");
var ArrayToolbar_1 = require("./ArrayToolbar");
var paperStyle = { padding: 10 };
var iconStyle = { float: 'right' };
var MaterialArrayLayout = /** @class */ (function (_super) {
    __extends(MaterialArrayLayout, _super);
    function MaterialArrayLayout() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            expanded: null
        };
        _this.innerCreateDefaultValue = function () { return core_1.createDefaultValue(_this.props.schema); };
        _this.handleChange = function (panel) { return function (_event, expanded) {
            _this.setState({
                expanded: expanded ? panel : false
            });
        }; };
        _this.isExpanded = function (index) {
            return _this.state.expanded === core_1.composePaths(_this.props.path, "" + index);
        };
        return _this;
    }
    MaterialArrayLayout.prototype.render = function () {
        var _this = this;
        var _a = this.props, data = _a.data, path = _a.path, schema = _a.schema, uischema = _a.uischema, errors = _a.errors, addItem = _a.addItem, renderers = _a.renderers, label = _a.label, required = _a.required;
        return (react_1.default.createElement(Paper_1.default, { style: paperStyle },
            react_1.default.createElement(ArrayToolbar_1.ArrayLayoutToolbar, { label: core_1.computeLabel(core_1.isPlainLabel(label) ? label : label.default, required), errors: errors, path: path, addItem: addItem, createDefault: this.innerCreateDefaultValue }),
            react_1.default.createElement("div", null, data > 0 ? (map_1.default(range_1.default(data), function (index) {
                return (react_1.default.createElement(ConnectedExpandPanelRenderer, { index: index, expanded: _this.isExpanded(index), schema: schema, path: path, handleExpansion: _this.handleChange, uischema: uischema, renderers: renderers, key: index }));
            })) : (react_1.default.createElement("p", null, "No data")))));
    };
    return MaterialArrayLayout;
}(react_1.default.Component));
exports.MaterialArrayLayout = MaterialArrayLayout;
var ExpandPanelRenderer = /** @class */ (function (_super) {
    __extends(ExpandPanelRenderer, _super);
    function ExpandPanelRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ExpandPanelRenderer.prototype.render = function () {
        var _a = this.props, index = _a.index, expanded = _a.expanded, childLabel = _a.childLabel, schema = _a.schema, childPath = _a.childPath, removeItems = _a.removeItems, path = _a.path, handleExpansion = _a.handleExpansion, uischema = _a.uischema, uischemas = _a.uischemas, renderers = _a.renderers;
        var foundUISchema = core_1.findUISchema(uischemas, schema, uischema.scope, path, undefined, uischema);
        return (react_1.default.createElement(ExpansionPanel_1.default, { expanded: expanded, onChange: handleExpansion(childPath) },
            react_1.default.createElement(ExpansionPanelSummary_1.default, { expandIcon: react_1.default.createElement(ExpandMore_1.default, null) },
                react_1.default.createElement(core_2.Grid, { container: true, alignItems: 'center' },
                    react_1.default.createElement(core_2.Grid, { item: true, xs: 11 },
                        react_1.default.createElement(core_2.Grid, { container: true, alignItems: 'center' },
                            react_1.default.createElement(core_2.Grid, { item: true, xs: 1 },
                                react_1.default.createElement(Avatar_1.default, { "aria-label": 'Index' }, index + 1)),
                            react_1.default.createElement(core_2.Grid, { item: true, xs: 2 }, childLabel))),
                    react_1.default.createElement(core_2.Grid, { item: true, xs: 1 },
                        react_1.default.createElement(core_2.Grid, { container: true, justify: 'flex-end' },
                            react_1.default.createElement(core_2.Grid, { item: true },
                                react_1.default.createElement(IconButton_1.default, { onClick: removeItems(path, [index]), style: iconStyle },
                                    react_1.default.createElement(Delete_1.default, null))))))),
            react_1.default.createElement(ExpansionPanelDetails_1.default, null,
                react_1.default.createElement(react_2.ResolvedJsonForms, { schema: schema, uischema: foundUISchema, path: childPath, key: childPath, renderers: renderers }))));
    };
    return ExpandPanelRenderer;
}(react_1.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.mapStateToExpandPanelProps = 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,
        childPath: childPath, uischemas: state.jsonforms.uischemas });
};
/**
 * Maps state to dispatch properties of an expand pandel control.
 *
 * @param dispatch the store's dispatch method
 * @returns {DispatchPropsOfArrayControl} dispatch props of an expand panel control
 */
exports.mapDispatchToExpandPanelProps = function (dispatch) { return ({
    removeItems: function (path, toDelete) { return function () {
        dispatch(core_1.update(path, function (array) {
            toDelete
                .sort()
                .reverse()
                .forEach(function (s) { return array.splice(s, 1); });
            return array;
        }));
    }; }
}); };
var ConnectedExpandPanelRenderer = react_redux_1.connect(exports.mapStateToExpandPanelProps, exports.mapDispatchToExpandPanelProps)(ExpandPanelRenderer);
//# sourceMappingURL=MaterialArrayLayout.js.map