"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 isEmpty_1 = __importDefault(require("lodash/isEmpty"));
var union_1 = __importDefault(require("lodash/union"));
var react_1 = require("@jsonforms/react");
var startCase_1 = __importDefault(require("lodash/startCase"));
var range_1 = __importDefault(require("lodash/range"));
var react_2 = __importDefault(require("react"));
var react_redux_1 = require("react-redux");
var core_1 = require("@material-ui/core");
var core_2 = require("@jsonforms/core");
var IconButton_1 = __importDefault(require("@material-ui/core/IconButton"));
var Delete_1 = __importDefault(require("@material-ui/icons/Delete"));
var NoBorderTableCell_1 = __importDefault(require("./NoBorderTableCell"));
var TableToolbar_1 = __importDefault(require("./TableToolbar"));
// we want a cell that doesn't automatically span
var styles = {
    fixedCell: {
        width: '50px',
        height: '50px',
        paddingLeft: 0,
        paddingRight: 0,
        textAlign: 'center'
    }
};
var generateCells = function (Cell, schema, rowPath) {
    if (schema.type === 'object') {
        return getValidColumnProps(schema).map(function (prop) {
            var cellPath = core_2.Paths.compose(rowPath, prop);
            var props = {
                propName: prop,
                schema: schema,
                rowPath: rowPath,
                cellPath: cellPath
            };
            return react_2.default.createElement(Cell, __assign({ key: cellPath }, props));
        });
    }
    else {
        // primitives
        var props = {
            schema: schema,
            rowPath: rowPath,
            cellPath: rowPath
        };
        return react_2.default.createElement(Cell, __assign({ key: rowPath }, props));
    }
};
var getValidColumnProps = function (scopedSchema) {
    if (scopedSchema.type === 'object') {
        return Object.keys(scopedSchema.properties).filter(function (prop) { return scopedSchema.properties[prop].type !== 'array'; });
    }
    // primitives
    return [''];
};
var EmptyTable = function (_a) {
    var numColumns = _a.numColumns;
    return (react_2.default.createElement(core_1.TableRow, null,
        react_2.default.createElement(NoBorderTableCell_1.default, { colSpan: numColumns },
            react_2.default.createElement(core_1.Typography, { align: 'center' }, "No data"))));
};
var TableHeaderCell = react_2.default.memo(function (_a) {
    var propName = _a.propName;
    return (react_2.default.createElement(core_1.TableCell, null, startCase_1.default(propName)));
});
var mapStateToNonEmptyCellProps = function (state, ownProps) {
    var path = ownProps.rowPath + (ownProps.schema.type === 'object' ? '.' + ownProps.propName : '');
    var errors = core_2.formatErrorMessage(union_1.default(core_2.getErrorAt(path, ownProps.schema)(state).map(function (error) { return error.message; })));
    return {
        rowPath: ownProps.rowPath,
        propName: ownProps.propName,
        schema: ownProps.schema,
        rootSchema: state.jsonforms.core.schema,
        errors: errors,
        path: path
    };
};
var controlWithoutLabel = function (scope) { return ({
    type: 'Control',
    scope: scope,
    label: false
}); };
var NonEmptyCellInner = /** @class */ (function (_super) {
    __extends(NonEmptyCellInner, _super);
    function NonEmptyCellInner() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    NonEmptyCellInner.prototype.render = function () {
        var _a = this.props, path = _a.path, propName = _a.propName, schema = _a.schema, rootSchema = _a.rootSchema, errors = _a.errors;
        var isValid = isEmpty_1.default(errors);
        return (react_2.default.createElement(NoBorderTableCell_1.default, null,
            schema.properties ? (react_2.default.createElement(react_1.DispatchCell, { schema: core_2.Resolve.schema(schema, "#/properties/" + propName, rootSchema), uischema: controlWithoutLabel("#/properties/" + propName), path: path })) : (react_2.default.createElement(react_1.DispatchCell, { schema: schema, uischema: controlWithoutLabel('#'), path: path })),
            react_2.default.createElement(core_1.FormHelperText, { error: !isValid }, !isValid && errors)));
    };
    return NonEmptyCellInner;
}(react_2.default.Component));
var NonEmptyCell = react_redux_1.connect(mapStateToNonEmptyCellProps)(NonEmptyCellInner);
var NonEmptyRow = react_2.default.memo(function (_a) {
    var childPath = _a.childPath, schema = _a.schema, rowData = _a.rowData, openDeleteDialog = _a.openDeleteDialog;
    return (react_2.default.createElement(core_1.TableRow, { key: childPath, hover: true },
        generateCells(NonEmptyCell, schema, childPath),
        react_2.default.createElement(NoBorderTableCell_1.default, { style: styles.fixedCell },
            react_2.default.createElement("div", { style: { display: 'flex', justifyContent: 'center' } },
                react_2.default.createElement(IconButton_1.default, { "aria-label": "Delete", onClick: function () { return openDeleteDialog(childPath, rowData); } },
                    react_2.default.createElement(Delete_1.default, null))))));
});
var TableRows = function (_a) {
    var data = _a.data, path = _a.path, schema = _a.schema, openDeleteDialog = _a.openDeleteDialog;
    var isEmptyTable = data === 0;
    if (isEmptyTable) {
        return react_2.default.createElement(EmptyTable, { numColumns: getValidColumnProps(schema).length + 1 });
    }
    return (react_2.default.createElement(react_2.default.Fragment, null, range_1.default(data).map(function (index) {
        var childPath = core_2.Paths.compose(path, "" + index);
        return (react_2.default.createElement(NonEmptyRow, { key: childPath, childPath: childPath, rowData: index, schema: schema, openDeleteDialog: openDeleteDialog }));
    })));
};
var MaterialTableControl = /** @class */ (function (_super) {
    __extends(MaterialTableControl, _super);
    function MaterialTableControl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.addItem = function (path, value) { return _this.props.addItem(path, value); };
        return _this;
    }
    MaterialTableControl.prototype.render = function () {
        var _a = this.props, label = _a.label, path = _a.path, schema = _a.schema, rootSchema = _a.rootSchema, uischema = _a.uischema, errors = _a.errors, openDeleteDialog = _a.openDeleteDialog, visible = _a.visible;
        var controlElement = uischema;
        var isObjectSchema = schema.type === 'object';
        var headerCells = isObjectSchema
            ? generateCells(TableHeaderCell, schema, path)
            : undefined;
        return (react_2.default.createElement(core_1.Hidden, { xsUp: !visible },
            react_2.default.createElement(core_1.Table, null,
                react_2.default.createElement(core_1.TableHead, null,
                    react_2.default.createElement(TableToolbar_1.default, { errors: errors, label: label, addItem: this.addItem, numColumns: isObjectSchema ? headerCells.length : 1, path: path, uischema: controlElement, schema: schema, rootSchema: rootSchema }),
                    isObjectSchema && (react_2.default.createElement(core_1.TableRow, null,
                        headerCells,
                        react_2.default.createElement(core_1.TableCell, null)))),
                react_2.default.createElement(core_1.TableBody, null,
                    react_2.default.createElement(TableRows, __assign({ openDeleteDialog: openDeleteDialog }, this.props))))));
    };
    return MaterialTableControl;
}(react_2.default.Component));
exports.MaterialTableControl = MaterialTableControl;
//# sourceMappingURL=MaterialTableControl.js.map