"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("@jsonforms/core");
const core_2 = require("@angular/core");
const ionic_angular_1 = require("ionic-angular");
const category_1 = require("./category/category");
const store_1 = require("@angular-redux/store");
const JsonFormsIonicLayout_1 = require("../JsonFormsIonicLayout");
let CategorizationLayoutRenderer = class CategorizationLayoutRenderer extends JsonFormsIonicLayout_1.JsonFormsIonicLayout {
    constructor(ngRedux) {
        super(ngRedux);
    }
    ngOnInit() {
        this.categories = this.getOwnProps().uischema.elements;
        const firstCategory = this.getOwnProps().uischema.elements[0];
        this.label = firstCategory.label;
        const state$ = this.connectLayoutToJsonForms(this.ngRedux, this.getOwnProps());
        this.subscription = state$.subscribe(state => {
            this.stateProps = state;
        });
        this.renderCategory(firstCategory);
    }
    renderCategory(category) {
        this.nav.setRoot(category_1.CategoryRenderer, { category: category });
        this.label = category.label;
    }
    canGoBack() {
        // FIXME: addToStack allows explicit control when to display the 'Go back' button,
        // FIXME: is there a better way to control this?
        const count = this.nav.getViews().reduce((acc, view) => acc + (view.data.addToNavStack ? 1 : 0), 0);
        return count > 1;
    }
    goBack() {
        return this.nav.pop();
    }
};
__decorate([
    core_2.ViewChild('categoryContent'),
    __metadata("design:type", ionic_angular_1.NavController)
], CategorizationLayoutRenderer.prototype, "nav", void 0);
CategorizationLayoutRenderer = __decorate([
    core_2.Component({
        selector: 'jsonforms-categorization-layout',
        template: `
      <ion-header>
          <ion-navbar>
              <button ion-button menuToggle="categorization-menu">
                  <ion-icon name="menu"></ion-icon>
              </button>
              <ion-title>
                  {{label}}
              </ion-title>
              <ion-buttons end>
                  <button ion-button *ngIf="canGoBack()" (click)="goBack()">
                      Go Back
                  </button>
              </ion-buttons>
          </ion-navbar>
      </ion-header>


      <ion-content>
          <!-- LHS menu -->
          <ion-menu side="left" [content]="categoryContent" id="categorization-menu">
              <ion-content>
                  <ion-list>
                      <button ion-item
                              *ngFor="let category of categories"
                              (click)="renderCategory(category)">
                          {{category.label}}
                      </button>
                  </ion-list>
              </ion-content>
          </ion-menu>
          <ion-nav #categoryContent></ion-nav>
      </ion-content>
  `
    }),
    __metadata("design:paramtypes", [store_1.NgRedux])
], CategorizationLayoutRenderer);
exports.CategorizationLayoutRenderer = CategorizationLayoutRenderer;
exports.isCategorization = (category) => category.type === 'Categorization';
exports.categorizationTester = core_1.rankWith(1, core_1.and(core_1.uiTypeIs('Categorization'), uischema => {
    const hasCategory = (categorization) => {
        if (_.isEmpty(categorization.elements)) {
            return false;
        }
        // all children of the categorization have to be categories
        return categorization.elements
            .map(elem => exports.isCategorization(elem) ? hasCategory(elem) : elem.type === 'Category')
            .reduce((prev, curr) => prev && curr, true);
    };
    return hasCategory(uischema);
}));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2F0ZWdvcml6YXRpb24tbGF5b3V0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vc3JjL2xheW91dHMvY2F0ZWdvcml6YXRpb24vY2F0ZWdvcml6YXRpb24tbGF5b3V0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7O0FBQUEsNEJBQTRCO0FBQzVCLDBDQVN5QjtBQUN6Qix3Q0FBcUQ7QUFDckQsaURBQThDO0FBQzlDLG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