import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';

import {AuthGuard} from 'm2-core';
import {M2ComponentDocsPluginComponent} from './m2-component-docs-plugin.component';
import {CD_ROUTING} from './config';
import {CdContentStubComponent} from './components/stub/cd-content-stub.component';
import {CdContentGuardService} from './service/cd-content-guard.service';
import {getContentRoutesData, getDefaultContentRouterLink} from './content-config';
import {CdApiOverviewComponent} from './components/content/api-overview/cd-api-overview.component';
import {CdCodeExampleComponent} from './components/content/code-example/cd-code-example.component';
import {CdNodeType} from './components/docs-tree/CdNode';


const routes: Routes = [
    {
        path: '', pathMatch: 'full', redirectTo: `${getDefaultContentRouterLink()}/${CD_ROUTING.TABS.DEMO}`
    },
    {
        path: '', component: M2ComponentDocsPluginComponent, canActivate: [AuthGuard], children: [
            ...getContentRoutesData().reduce((res, [routerLink, demoCmp, cdNodeType, children]) => {
                return res.concat(
                    {
                        path: routerLink,
                        // pathMatch: 'full', redirectTo: `${routerLink}/${CD_ROUTING.TABS.DEMO}`, // // redirection is implemented in guard service
                        canActivate: [CdContentGuardService],
                        data: { cdNodeType }
                    },
                    {
                        path: `${routerLink}/${cdNodeType === CdNodeType.group ? CD_ROUTING.TABS.GROUP : CD_ROUTING.TABS.DEMO}`,
                        component: demoCmp,
                        children
                    }
                );
            }, [] as Routes),

            { path: `:${CD_ROUTING.PARAMS.SECTION}/${CD_ROUTING.TABS.CODE}`, component: CdCodeExampleComponent },
            { path: `:${CD_ROUTING.PARAMS.SECTION}/${CD_ROUTING.TABS.API}`, component: CdApiOverviewComponent },

            { path: '**', component: CdContentStubComponent }
        ]
    }
];


@NgModule({
    imports: [
        RouterModule.forChild(routes)
    ],
    exports: [
        RouterModule
    ]
})
export class M2ComponentDocsPluginRoutingModule {

}
