import { RouterModule, Routes } from '@angular/router';
import { TldListComponent } from './tld-list/tld-list.component';
import { TLDListResolver } from '@fleio-api/plugins/domains/tld-list.resolver';
import { AuthGuard } from '@shared/auth/auth.guard';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { TldCreateComponent } from './tld-create/tld-create.component';
import { TLDResolver } from '@fleio-api/plugins/domains/tld.resolver';
import { TldDetailsComponent } from './tld-details/tld-details.component';
import { NgModule } from '@angular/core';
import { TLDPricesResolver } from '@fleio-api/plugins/domains/tld-prices.resolver';

const routes: Routes = [
  {
    path: '',
    component: TldListComponent,
    resolve: {
      tlds: TLDListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'plugins.domains',
        search: {
          show: true,
          placeholder: $localize`Search TLDs ...`,
        },
        subheader: {
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.tlds;
          },
          objectName: $localize`tld`,
          objectNamePlural: $localize`tlds`,
        },
        ordering: {
          default: {
            display: $localize`Created at`,
            field: 'created_at',
            direction: OrderingDirection.Descending,
          },
          options: [
            {
              display: $localize`Created at`,
              field: 'created_at',
            },
            {
              display: $localize`Name`,
              field: 'name',
            },
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              field: 'created_at',
              display: $localize`Created at`,
              type: FilterTypes.Date,
            },
            {
              field: 'premium_domains_available',
              display: $localize`Premium domains available`,
              type: FilterTypes.Boolean,
            },
            {
              field: 'requires_epp_for_transfer',
              display: $localize`Requires epp for transfer`,
              type: FilterTypes.Boolean,
            },
            {
              field: 'default_registrar',
              display: $localize`Default registrar`,
              type: FilterTypes.CustomModel,
              items: 'domainRegistrars',
              itemsDisplayField: 'name',
            },
          ],
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'create',
    component: TldCreateComponent,
    resolve: {
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create TLD`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id',
    component: TldDetailsComponent,
    resolve: {
      tld: TLDResolver,
      prices: TLDPricesResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.tld.name,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
];

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