"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ɵ0 = exports.TableRendererTester = exports.TableRenderer = void 0;
var tslib_1 = require("tslib");
/*
  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 startCase_1 = tslib_1.__importDefault(require("lodash/startCase"));
var core_1 = require("@angular/core");
var angular_1 = require("@jsonforms/angular");
var core_2 = require("@jsonforms/core");
var TableRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(TableRenderer, _super);
    function TableRenderer(jsonformsService) {
        var _this = _super.call(this, jsonformsService) || this;
        _this.columnsToIgnore = ['array', 'object'];
        _this.generateCells = function (schema, rowPath) {
            if (schema.type === 'object') {
                return _this.getValidColumnProps(schema).map(function (prop) {
                    var uischema = controlWithoutLabel("#/properties/" + prop);
                    if (!_this.isEnabled()) {
                        core_2.setReadonly(uischema);
                    }
                    return {
                        property: prop,
                        header: startCase_1.default(prop),
                        props: {
                            schema: schema,
                            uischema: uischema,
                            path: rowPath
                        }
                    };
                });
            }
            // needed to correctly render input control for multi attributes
            return [
                {
                    property: 'DUMMY',
                    header: _this.label,
                    props: {
                        schema: schema,
                        uischema: controlWithoutLabel("#"),
                        path: rowPath
                    }
                }
            ];
        };
        _this.getValidColumnProps = function (scopedSchema) {
            if (scopedSchema.type === 'object') {
                return Object.keys(scopedSchema.properties).filter(function (prop) {
                    var types = core_2.deriveTypes(scopedSchema.properties[prop]);
                    if (types.length > 1) {
                        return false;
                    }
                    return _this.columnsToIgnore.indexOf(types[0]) === -1;
                });
            }
            // primitives
            return [''];
        };
        return _this;
    }
    TableRenderer.prototype.trackElement = function (index, _element) {
        return index ? index : null;
    };
    TableRenderer.prototype.mapAdditionalProps = function (props) {
        this.items = this.generateCells(props.schema, props.path);
        this.displayedColumns = this.items.map(function (item) { return item.property; });
    };
    TableRenderer.prototype.getProps = function (index, props) {
        var rowPath = core_2.Paths.compose(props.path, "" + index);
        return {
            schema: props.schema,
            uischema: props.uischema,
            path: rowPath
        };
    };
    TableRenderer.decorators = [
        { type: core_1.Component, args: [{
                    selector: 'TableRenderer',
                    template: "\n    <table\n      mat-table\n      [dataSource]=\"data\"\n      class=\"mat-elevation-z8\"\n      [trackBy]=\"trackElement\"\n    >\n      <ng-container\n        *ngFor=\"let item of items\"\n        matColumnDef=\"{{ item.property }}\"\n      >\n        <th mat-header-cell *matHeaderCellDef>{{ item.header }}</th>\n        <td mat-cell *matCellDef=\"let index = index\">\n          <jsonforms-outlet\n            [renderProps]=\"getProps(index, item.props)\"\n          ></jsonforms-outlet>\n        </td>\n      </ng-container>\n\n      <tr mat-header-row *matHeaderRowDef=\"displayedColumns\"></tr>\n      <tr mat-row *matRowDef=\"let row; columns: displayedColumns\"></tr>\n    </table>\n  ",
                    styles: ['table {width: 100%;}']
                },] }
    ];
    TableRenderer.ctorParameters = function () { return [
        { type: angular_1.JsonFormsAngularService }
    ]; };
    return TableRenderer;
}(angular_1.JsonFormsArrayControl));
exports.TableRenderer = TableRenderer;
exports.TableRendererTester = core_2.rankWith(3, core_2.or(core_2.isObjectArrayControl, core_2.isPrimitiveArrayControl));
var controlWithoutLabel = function (scope) { return ({
    type: 'Control',
    scope: scope,
    label: false
}); };
var ɵ0 = controlWithoutLabel;
exports.ɵ0 = ɵ0;
//# sourceMappingURL=table.renderer.js.map