import { getModelPaths, getViewPaths } from './templates/paths';
import type { Base, Model, Source } from '~/models';
import type { SwaggerColumn } from './getSwaggerColumnMetas';
import type { SwaggerView } from '~/services/api-docs/shared/swaggerUtils';
import type { NcContext } from '~/interface/config';
import Noco from '~/Noco';

export default async function getPaths(
  context: NcContext,
  {
    base,
    model,
    columns,
    views,
    tableName,
  }: {
    base: Base;
    model: Model;
    columns: SwaggerColumn[];
    views: SwaggerView[];
    sourcesMap: Map<string, Source>;
    tableName: string;
  },
  _ncMeta = Noco.ncMeta,
) {
  const swaggerPaths = await getModelPaths(context, {
    tableName,
    tableId: model.id,
    type: model.type,
    orgs: 'v1',
    columns,
    baseName: base.id,
  });

  for (const { view, columns: viewColumns } of views) {
    const swaggerColumns = columns.filter(
      (c) => viewColumns.find((vc) => vc.fk_column_id === c.column.id)?.show,
    );
    Object.assign(
      swaggerPaths,
      await getViewPaths(context, {
        tableName,
        tableId: model.id,
        viewName: view.title,
        viewId: view.id,
        type: model.type,
        orgs: 'v1',
        columns: swaggerColumns,
        baseName: base.id,
      }),
    );
  }

  return swaggerPaths;
}
