"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayLayoutRendererTester = exports.ArrayLayoutRenderer = void 0;
var tslib_1 = require("tslib");
/*
  The MIT License

  Copyright (c) 2017-2020 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("@angular/core");
var angular_1 = require("@jsonforms/angular");
var core_2 = require("@jsonforms/core");
var ArrayLayoutRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(ArrayLayoutRenderer, _super);
    function ArrayLayoutRenderer(jsonFormsService) {
        return _super.call(this, jsonFormsService) || this;
    }
    ArrayLayoutRenderer.prototype.mapToProps = function (state) {
        var props = core_2.mapStateToArrayLayoutProps(state, this.getOwnProps());
        return tslib_1.__assign({}, props);
    };
    ArrayLayoutRenderer.prototype.remove = function (index) {
        this.removeItems(this.propsPath, [index])();
    };
    ArrayLayoutRenderer.prototype.add = function () {
        this.addItem(this.propsPath, core_2.createDefaultValue(this.scopedSchema))();
    };
    ArrayLayoutRenderer.prototype.ngOnInit = function () {
        _super.prototype.ngOnInit.call(this);
        var _a = core_2.mapDispatchToArrayControlProps(this.jsonFormsService.updateCore.bind(this.jsonFormsService)), addItem = _a.addItem, removeItems = _a.removeItems;
        this.addItem = addItem;
        this.removeItems = removeItems;
    };
    ArrayLayoutRenderer.prototype.mapAdditionalProps = function (props) {
        this.noData = !props.data || props.data === 0;
        this.uischemas = props.uischemas;
        this.addTooltip = "Add to " + this.label;
        this.addAriaLabel = "Add to " + this.label + " button";
        this.removeTooltip = "Delete";
        this.removeAriaLabel = "Delete button";
        this.noDataMessage = "No data";
    };
    ArrayLayoutRenderer.prototype.getProps = function (index) {
        var uischema = core_2.findUISchema(this.uischemas, this.scopedSchema, this.uischema.scope, this.propsPath, undefined, this.uischema);
        if (this.isEnabled()) {
            core_2.unsetReadonly(uischema);
        }
        else {
            core_2.setReadonly(uischema);
        }
        return {
            schema: this.scopedSchema,
            path: core_2.Paths.compose(this.propsPath, "" + index),
            uischema: uischema
        };
    };
    ArrayLayoutRenderer.prototype.trackByFn = function (index) {
        return index;
    };
    ArrayLayoutRenderer.decorators = [
        { type: core_1.Component, args: [{
                    selector: 'app-array-layout-renderer',
                    template: "\n    <div fxLayout=\"column\" fxLayoutGap=\"16px\" [fxHide]=\"hidden\">\n      <div [ngClass]=\"'array-layout-toolbar'\">\n        <h2 [ngClass]=\"['mat-h2', 'array-layout-title']\">{{ label }}</h2>\n        <span fxFlex></span>\n        <mat-icon\n          *ngIf=\"this.error?.length\"\n          color=\"warn\"\n          matBadge=\"{{ this.error.split('\n').length }}\"\n          matBadgeColor=\"warn\"\n          matTooltip=\"{{ this.error }}\"\n          matTooltipClass=\"error-message-tooltip\"\n          >\n            error_outline\n        </mat-icon>\n        <span fxFlex></span>\n        <button\n          mat-button\n          matTooltip=\"{{ this.addTooltip }}\"\n          [disabled]=\"!isEnabled()\"\n          (click)=\"add()\"\n          attr.aria-label=\"{{ this.addAriaLabel }}\"\n        >\n          <mat-icon>add</mat-icon>\n        </button>\n      </div>\n      <p *ngIf=\"noData\">{{ this.noDataMessage }}</p>\n      <div\n        *ngFor=\"\n          let item of [].constructor(data);\n          let idx = index;\n          trackBy: trackByFn;\n          last as last\n        \"\n      >\n        <mat-card>\n          <mat-card-content>\n            <jsonforms-outlet [renderProps]=\"getProps(idx)\"></jsonforms-outlet>\n          </mat-card-content>\n          <mat-card-actions *ngIf=\"isEnabled()\">\n            <button\n              mat-button\n              color=\"warn\"\n              (click)=\"remove(idx)\"\n              attr.aria-label=\"{{ this.removeAriaLabel }}\"\n              matTooltip=\"{{ this.removeTooltip }}\"\n              matTooltipPosition=\"right\"\n            >\n              <mat-icon>delete</mat-icon>\n            </button>\n          </mat-card-actions>\n        </mat-card>\n      </div>\n    </div>\n  ",
                    styles: [
                        ".array-layout-toolbar {\n       display: flex;\n       align-items: center;\n      }\n      .array-layout-title {\n        margin: 0;\n      }\n      ::ng-deep .error-message-tooltip {\n        white-space: pre-line;\n      }\n      "
                    ],
                    changeDetection: core_1.ChangeDetectionStrategy.OnPush
                },] }
    ];
    ArrayLayoutRenderer.ctorParameters = function () { return [
        { type: angular_1.JsonFormsAngularService }
    ]; };
    return ArrayLayoutRenderer;
}(angular_1.JsonFormsAbstractControl));
exports.ArrayLayoutRenderer = ArrayLayoutRenderer;
exports.ArrayLayoutRendererTester = core_2.rankWith(4, core_2.isObjectArrayWithNesting);
//# sourceMappingURL=array-layout.renderer.js.map