"use strict";
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 });
const _ = require("lodash");
const core_1 = require("@angular/core");
const core_2 = require("@jsonforms/core");
const angular_1 = require("@jsonforms/angular");
const ionic_angular_1 = require("ionic-angular");
const master_detail_nav_service_1 = require("./master-detail-nav.service");
const master_1 = require("./pages/master/master");
const common_1 = require("../../common");
const store_1 = require("@angular-redux/store");
const detail_1 = require("./pages/detail/detail");
let MasterDetailComponent = class MasterDetailComponent extends angular_1.JsonFormsControl {
    constructor(parentNav, platform, ngRedux, masterDetailService) {
        super(ngRedux);
        this.parentNav = parentNav;
        this.platform = platform;
        this.masterDetailService = masterDetailService;
    }
    ngOnInit() {
        this.masterDetailService.masterNav = this.masterNav;
        this.masterDetailService.detailNav = this.detailNav;
        this.masterDetailService.parentNav = this.parentNav;
        const state$ = this.connectControlToJsonForms(this.ngRedux, this.getOwnProps());
        this.subscription = state$.subscribe(state => {
            const controlElement = state.uischema;
            const labelRefInstancePath = common_1.removeSchemaKeywords(controlElement.options.labelRef);
            const instancePath = core_2.toDataPath(`${controlElement.scope}/items`);
            const resolvedSchema = core_2.resolveSchema(state.schema, `${controlElement.scope}/items`);
            const detailUISchema = controlElement.options.detail ||
                core_2.Generate.uiSchema(resolvedSchema, 'VerticalLayout');
            this.masterItems = state.data.map((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;
        }
    }
    onSplitPaneChange(event) {
        const isDetailVisible = event._visible;
        this.masterDetailService.onDetailVisibilityChanged(isDetailVisible, this.parentNav);
    }
    ngOnDestroy() {
        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: `
      <ion-split-pane (ionChange)="onSplitPaneChange($event)">
          <ion-nav [root]="masterPage" #masterNav ></ion-nav>
          <ion-nav [root]="detailPage" #detailNav main></ion-nav>
      </ion-split-pane>
  `
    }),
    __metadata("design:paramtypes", [ionic_angular_1.Nav,
        ionic_angular_1.Platform,
        store_1.NgRedux,
        master_detail_nav_service_1.MasterDetailNavService])
], MasterDetailComponent);
exports.MasterDetailComponent = MasterDetailComponent;
exports.masterDetailTester = core_2.rankWith(4, core_2.uiTypeIs('FlatMasterDetail'));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFzdGVyLWRldGFpbC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uL3NyYy9vdGhlci9tYXN0ZXItZGV0YWlsL21hc3Rlci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