import { __decorate } from "tslib";
import { Component, ChangeDetectionStrategy, InjectionToken, ViewChild } from '@angular/core';
import { MatTabGroup, MatTabsModule } from '@angular/material/tabs';
import { CommonModule } from '@angular/common';
import { LayoutComponentBase, LayoutComponentRendererSADirective } from '@anglr/dynamic/layout';
import { DescendantsGetter, LayoutEditorMetadata } from '@anglr/dynamic/layout-editor';
import { HostDisplayBlockStyle } from '@anglr/common';
import { generateId } from '@jscrpt/common';
import { MaterialTabGroupLayoutMetadataLoader } from './tabGroup.metadata';
import { MaterialTabSAComponent } from '../tab/tab.component';
import * as i0 from "@angular/core";
import * as i1 from "@angular/common";
import * as i2 from "@angular/material/tabs";
/**
 * Injection token used for injecting current scope id
 */
export const MATERIAL_TAB_GROUP = new InjectionToken('MATERIAL_TAB_GROUP');
/**
 * Component used for displaying toggle panel layout
 */
let MaterialTabGroupSAComponent = class MaterialTabGroupSAComponent extends LayoutComponentBase {
    //######################### public properties - inputs #########################
    /**
     * @inheritdoc
     */
    onOptionsSet() {
        var _a;
        if (this.relationsOptions) {
            this.relationsOptions.tabs = (_a = this.options) === null || _a === void 0 ? void 0 : _a.tabs;
        }
    }
    /**
     * Handles tab content change
     * @param componentRef Tab component ref
     * @param index index in tab group
     */
    onTabComponentChange(componentRef, index) {
        if (componentRef === null || componentRef === void 0 ? void 0 : componentRef.instance) {
            componentRef.instance.tabGroupIndex = index;
            componentRef.instance.invalidateVisuals();
        }
    }
    //######################### public methods #########################
    /**
     * Handles tab visibility in group
     * @param index
     * @returns
     */
    onHideTab(index) {
        var _a, _b;
        if (((_a = this.tabGroup) === null || _a === void 0 ? void 0 : _a.selectedIndex) === index &&
            ((_b = this.tabGroup) === null || _b === void 0 ? void 0 : _b._tabs)) {
            for (let i = 0; i < this.tabGroup._tabs.length; i++) {
                const tab = this.tabGroup._tabs.get(i);
                if (!(tab === null || tab === void 0 ? void 0 : tab.disabled)) {
                    this.tabGroup.selectedIndex = i;
                    return;
                }
            }
        }
        this.invalidateVisuals();
    }
    onInit() {
        var _a;
        if (this.options &&
            !((_a = this.options) === null || _a === void 0 ? void 0 : _a.tabs)) {
            this.options.tabs = [
                {
                    title: 'Tab',
                    content: {
                        id: `${generateId(6)}-tab`,
                        package: 'material-components',
                        name: 'tab',
                        options: {}
                    }
                }
            ];
        }
    }
};
MaterialTabGroupSAComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: MaterialTabGroupSAComponent, deps: null, target: i0.ɵɵFactoryTarget.Component });
MaterialTabGroupSAComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.0.4", type: MaterialTabGroupSAComponent, isStandalone: true, selector: "material-tab-group", providers: [
        {
            provide: MATERIAL_TAB_GROUP,
            useExisting: MaterialTabGroupSAComponent,
        }
    ], viewQueries: [{ propertyName: "tabGroup", first: true, predicate: MatTabGroup, descendants: true }], usesInheritance: true, ngImport: i0, template: "<mat-tab-group [mat-stretch-tabs]=\"false\">\n    <mat-tab #tabComponent=\"matTab\" [label]=\"tab.title\" *ngFor=\"let tab of options?.tabs; let i = index;\" [labelClass]=\"tabComponent.disabled ? 'hidden': ''\" [bodyClass]=\"tabComponent.disabled ? 'hidden': ''\">\n        <ng-template [layoutComponentRenderer]=\"tab.content\" (layoutComponentRendererComponentChange)=\"onTabComponentChange($event, i)\"></ng-template>\n    </mat-tab>\n</mat-tab-group>", styles: [":host{display: block;}"], dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i1.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "ngmodule", type: MatTabsModule }, { kind: "component", type: i2.MatTab, selector: "mat-tab", inputs: ["disabled", "label", "aria-label", "aria-labelledby", "labelClass", "bodyClass"], exportAs: ["matTab"] }, { kind: "component", type: i2.MatTabGroup, selector: "mat-tab-group", inputs: ["color", "disableRipple", "fitInkBarToContent", "mat-stretch-tabs", "dynamicHeight", "selectedIndex", "headerPosition", "animationDuration", "contentTabIndex", "disablePagination", "preserveContent", "backgroundColor"], outputs: ["selectedIndexChange", "focusChange", "animationDone", "selectedTabChange"], exportAs: ["matTabGroup"] }, { kind: "directive", type: LayoutComponentRendererSADirective, selector: "[layoutComponentRenderer]", inputs: ["layoutComponentRenderer", "layoutComponentRendererInjector", "layoutComponentRendererDisableTransformer"], outputs: ["layoutComponentRendererComponentChange", "componentElementChange"], exportAs: ["layoutComponentRenderer"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
MaterialTabGroupSAComponent = __decorate([
    DescendantsGetter(options => (options === null || options === void 0 ? void 0 : options.tabs) ? options === null || options === void 0 ? void 0 : options.tabs.map(datum => datum.content) : []),
    LayoutEditorMetadata(MaterialTabGroupLayoutMetadataLoader)
], MaterialTabGroupSAComponent);
export { MaterialTabGroupSAComponent };
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: MaterialTabGroupSAComponent, decorators: [{
            type: Component,
            args: [{ selector: 'material-tab-group', standalone: true, imports: [
                        CommonModule,
                        MatTabsModule,
                        MaterialTabSAComponent,
                        LayoutComponentRendererSADirective,
                    ], providers: [
                        {
                            provide: MATERIAL_TAB_GROUP,
                            useExisting: MaterialTabGroupSAComponent,
                        }
                    ], changeDetection: ChangeDetectionStrategy.OnPush, template: "<mat-tab-group [mat-stretch-tabs]=\"false\">\n    <mat-tab #tabComponent=\"matTab\" [label]=\"tab.title\" *ngFor=\"let tab of options?.tabs; let i = index;\" [labelClass]=\"tabComponent.disabled ? 'hidden': ''\" [bodyClass]=\"tabComponent.disabled ? 'hidden': ''\">\n        <ng-template [layoutComponentRenderer]=\"tab.content\" (layoutComponentRendererComponentChange)=\"onTabComponentChange($event, i)\"></ng-template>\n    </mat-tab>\n</mat-tab-group>", styles: [":host{display: block;}"] }]
        }], propDecorators: { tabGroup: [{
                type: ViewChild,
                args: [MatTabGroup]
            }] } });
//# sourceMappingURL=tabGroup.component.js.map