"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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 some_1 = tslib_1.__importDefault(require("lodash/some"));
var get_1 = tslib_1.__importDefault(require("lodash/get"));
var core_1 = require("@angular/core");
var angular_1 = require("@jsonforms/angular");
var core_2 = require("@jsonforms/core");
var keywords = ['#', 'properties', 'items'];
exports.removeSchemaKeywords = function (path) {
    return path
        .split('/')
        .filter(function (s) { return !some_1.default(keywords, function (key) { return key === s; }); })
        .join('.');
};
var MasterListComponent = /** @class */ (function (_super) {
    tslib_1.__extends(MasterListComponent, _super);
    function MasterListComponent(jsonformsService) {
        return _super.call(this, jsonformsService) || this;
    }
    MasterListComponent.prototype.onListItemHover = function (idx) {
        this.highlightedIdx = idx;
    };
    MasterListComponent.prototype.trackElement = function (_index, element) {
        return element ? element.label : null;
    };
    MasterListComponent.prototype.ngOnInit = function () {
        _super.prototype.ngOnInit.call(this);
        var dispatch = this.jsonFormsService.updateCore.bind(this.jsonFormsService);
        var _a = core_2.mapDispatchToArrayControlProps(dispatch), addItem = _a.addItem, removeItems = _a.removeItems;
        this.addItem = addItem;
        this.removeItems = removeItems;
    };
    MasterListComponent.prototype.mapAdditionalProps = function (props) {
        var data = props.data, path = props.path, schema = props.schema, uischema = props.uischema;
        var controlElement = uischema;
        this.propsPath = props.path;
        var detailUISchema = controlElement.options.detail ||
            core_2.findUISchema(props.uischemas, schema, controlElement.scope + "/items", props.path, 'VerticalLayout');
        if (!this.isEnabled()) {
            core_2.setReadonly(detailUISchema);
        }
        var masterItems = (data || []).map(function (d, index) {
            var labelRefInstancePath = exports.removeSchemaKeywords(controlElement.options.labelRef);
            var masterItem = {
                label: get_1.default(d, labelRefInstancePath),
                data: d,
                path: path + "." + index,
                schema: schema,
                uischema: detailUISchema
            };
            return masterItem;
        });
        this.masterItems = masterItems;
        var newSelectedIdx = -1;
        var newSelectedItem;
        if (this.masterItems.length === 0) {
            // unset select if no elements anymore
            this.selectedItem = undefined;
            this.selectedItemIdx = -1;
        }
        else if (this.selectedItemIdx >= this.masterItems.length) {
            // the previous index is to high, reduce it to the maximal possible
            newSelectedIdx = this.masterItems.length - 1;
            newSelectedItem = this.masterItems[newSelectedIdx];
        }
        else if (this.selectedItemIdx !== -1 &&
            this.selectedItemIdx < this.masterItems.length) {
            newSelectedIdx = this.selectedItemIdx;
            newSelectedItem = this.masterItems[this.selectedItemIdx];
        }
        if (newSelectedItem !== undefined &&
            this.selectedItem !== undefined &&
            (newSelectedItem.label === this.selectedItem.label ||
                newSelectedItem.path === this.selectedItem.path)) {
            // after checking that we are on the same path, set selection
            this.selectedItem = newSelectedItem;
            this.selectedItemIdx = newSelectedIdx;
        }
        else if (this.masterItems.length > 0) {
            // pre-select 1st entry if the previous selected element as fallback
            this.selectedItem = this.masterItems[0];
            this.selectedItemIdx = 0;
        }
    };
    MasterListComponent.prototype.onSelect = function (item, idx) {
        this.selectedItem = item;
        this.selectedItemIdx = idx;
    };
    MasterListComponent.prototype.onAddClick = function () {
        this.addItem(this.propsPath, core_2.createDefaultValue(this.scopedSchema))();
    };
    MasterListComponent.prototype.onDeleteClick = function (item) {
        this.removeItems(this.propsPath, [item])();
    };
    MasterListComponent.prototype.mapToProps = function (state) {
        var props = core_2.mapStateToArrayControlProps(state, this.getOwnProps());
        return tslib_1.__assign({}, props);
    };
    MasterListComponent = tslib_1.__decorate([
        core_1.Component({
            selector: 'jsonforms-list-with-detail-master',
            template: "\n    <mat-sidenav-container class=\"container\" [fxHide]=\"hidden\">\n      <mat-sidenav mode=\"side\" opened>\n        <mat-nav-list>\n          <mat-list-item *ngIf=\"masterItems.length === 0\"\n            >No items</mat-list-item\n          >\n          <mat-list-item\n            *ngFor=\"\n              let item of masterItems;\n              let i = index;\n              trackBy: trackElement\n            \"\n            [class.selected]=\"item === selectedItem\"\n            (click)=\"onSelect(item, i)\"\n            (mouseover)=\"onListItemHover(i)\"\n            (mouseout)=\"onListItemHover(undefined)\"\n          >\n            <a matLine>{{ item.label || 'No label set' }}</a>\n            <button\n              mat-icon-button\n              class=\"button hide\"\n              (click)=\"onDeleteClick(i)\"\n              [ngClass]=\"{ show: highlightedIdx == i }\"\n              *ngIf=\"isEnabled()\"\n            >\n              <mat-icon mat-list-icon>delete</mat-icon>\n            </button>\n          </mat-list-item>\n        </mat-nav-list>\n        <button\n          mat-fab\n          color=\"primary\"\n          class=\"add-button\"\n          (click)=\"onAddClick()\"\n          *ngIf=\"isEnabled()\"\n        >\n          <mat-icon aria-label=\"Add item to list\">add</mat-icon>\n        </button>\n      </mat-sidenav>\n      <mat-sidenav-content class=\"content\">\n        <jsonforms-detail\n          *ngIf=\"selectedItem\"\n          [item]=\"selectedItem\"\n        ></jsonforms-detail>\n      </mat-sidenav-content>\n    </mat-sidenav-container>\n  ",
            styles: [
                "\n      mat-list-item.selected {\n        background: rgba(0, 0, 0, 0.04);\n      }\n      .container {\n        height: 100vh;\n      }\n      .content {\n        padding: 15px;\n        background-color: #fff;\n      }\n      .add-button {\n        float: right;\n        margin-top: 0.5em;\n        margin-right: 0.25em;\n      }\n      .button {\n        float: right;\n        margin-right: 0.25em;\n      }\n      .hide {\n        display: none;\n      }\n      .show {\n        display: inline-block;\n      }\n      mat-sidenav {\n        width: 20%;\n      }\n    "
            ],
            changeDetection: core_1.ChangeDetectionStrategy.OnPush
        }),
        tslib_1.__metadata("design:paramtypes", [angular_1.JsonFormsAngularService])
    ], MasterListComponent);
    return MasterListComponent;
}(angular_1.JsonFormsArrayControl));
exports.MasterListComponent = MasterListComponent;
exports.masterDetailTester = core_2.rankWith(4, core_2.uiTypeIs('ListWithDetail'));
//# sourceMappingURL=master.js.map