"use strict";
var __extends = (this && this.__extends) || (function () {
    var 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 function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var core_1 = require("@angular/core");
var core_2 = require("@jsonforms/core");
var angular_1 = require("@jsonforms/angular");
var ionic_angular_1 = require("ionic-angular");
var master_detail_nav_service_1 = require("./master-detail-nav.service");
var master_1 = require("./pages/master/master");
var common_1 = require("../../common");
var store_1 = require("@angular-redux/store");
var detail_1 = require("./pages/detail/detail");
var MasterDetailComponent = /** @class */ (function (_super) {
    __extends(MasterDetailComponent, _super);
    function MasterDetailComponent(parentNav, platform, ngRedux, masterDetailService) {
        var _this = _super.call(this, ngRedux) || this;
        _this.parentNav = parentNav;
        _this.platform = platform;
        _this.masterDetailService = masterDetailService;
        return _this;
    }
    MasterDetailComponent.prototype.ngOnInit = function () {
        var _this = this;
        this.masterDetailService.masterNav = this.masterNav;
        this.masterDetailService.detailNav = this.detailNav;
        this.masterDetailService.parentNav = this.parentNav;
        var state$ = this.connectControlToJsonForms(this.ngRedux, this.getOwnProps());
        this.subscription = state$.subscribe(function (state) {
            var controlElement = state.uischema;
            var labelRefInstancePath = common_1.removeSchemaKeywords(controlElement.options.labelRef);
            var instancePath = core_2.toDataPath(controlElement.scope + "/items");
            var resolvedSchema = core_2.resolveSchema(state.schema, controlElement.scope + "/items");
            var detailUISchema = controlElement.options.detail ||
                core_2.Generate.uiSchema(resolvedSchema, 'VerticalLayout');
            _this.masterItems = state.data.map(function (d, index) {
                return {
                    label: _.get(d, labelRefInstancePath),
                    data: d,
                    path: instancePath + "." + index,
                    schema: resolvedSchema,
                    uischema: detailUISchema
                };
            });
        });
        this.masterDetailService.masterNav.setRoot(master_1.MasterPage, {
            addToNavStack: true,
            items: this.masterItems
        });
        this.detailNav.setRoot(detail_1.DetailPage, {
            addToNavStack: true,
            // TODO: must have items
            item: this.masterItems[0]
        });
        if (this.platform.width() < 768) {
            this.masterDetailService.useParentNav = true;
            this.parentNav.push(master_1.MasterPage, {
                addToNavStack: true,
                items: this.masterItems
            });
        }
        else {
            this.masterDetailService.useParentNav = false;
        }
    };
    MasterDetailComponent.prototype.onSplitPaneChange = function (event) {
        var isDetailVisible = event._visible;
        this.masterDetailService.onDetailVisibilityChanged(isDetailVisible, this.parentNav);
    };
    MasterDetailComponent.prototype.ngOnDestroy = function () {
        this.subscription.unsubscribe();
    };
    __decorate([
        core_1.ViewChild('masterNav'),
        __metadata("design:type", ionic_angular_1.Nav)
    ], MasterDetailComponent.prototype, "masterNav", void 0);
    __decorate([
        core_1.ViewChild('detailNav'),
        __metadata("design:type", ionic_angular_1.Nav)
    ], MasterDetailComponent.prototype, "detailNav", void 0);
    MasterDetailComponent = __decorate([
        core_1.Component({
            selector: 'jsonforms-master-detail',
            template: "\n      <ion-split-pane (ionChange)=\"onSplitPaneChange($event)\">\n          <ion-nav [root]=\"masterPage\" #masterNav ></ion-nav>\n          <ion-nav [root]=\"detailPage\" #detailNav main></ion-nav>\n      </ion-split-pane>\n  "
        }),
        __metadata("design:paramtypes", [ionic_angular_1.Nav,
            ionic_angular_1.Platform,
            store_1.NgRedux,
            master_detail_nav_service_1.MasterDetailNavService])
    ], MasterDetailComponent);
    return MasterDetailComponent;
}(angular_1.JsonFormsControl));
exports.MasterDetailComponent = MasterDetailComponent;
exports.masterDetailTester = core_2.rankWith(4, core_2.uiTypeIs('FlatMasterDetail'));
//# sourceMappingURL=master-detail.js.map