"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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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 some_1 = __importDefault(require("lodash/some"));
var get_1 = __importDefault(require("lodash/get"));
var core_1 = require("@angular/core");
var angular_1 = require("@jsonforms/angular");
var store_1 = require("@angular-redux/store");
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) {
    __extends(MasterListComponent, _super);
    function MasterListComponent(ngRedux) {
        return _super.call(this, ngRedux) || 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 _a = core_2.mapDispatchToArrayControlProps(this.ngRedux.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');
        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) {
            // 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());
        var dispatch = core_2.mapDispatchToArrayControlProps(this.ngRedux.dispatch);
        return __assign({}, props, dispatch);
    };
    MasterListComponent = __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            >\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        >\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    "
            ]
        }),
        __metadata("design:paramtypes", [store_1.NgRedux])
    ], MasterListComponent);
    return MasterListComponent;
}(angular_1.JsonFormsArrayControl));
exports.MasterListComponent = MasterListComponent;
exports.masterDetailTester = core_2.rankWith(4, core_2.uiTypeIs('ListWithDetail'));
//# sourceMappingURL=master.js.map