import { useMemo } from 'react';
import type { Action } from '@ablogcms/components/dataview/types';
import { getDataviewActionRegistry } from '@ablogcms/plugins/core';
import type { ModuleType } from '../types';

export interface GetActionsContext {
  invalidate: () => void;
  searchParams: URLSearchParams;
}

export interface UseModuleAdminActionsOptions {
  getActions: (actions: Action<ModuleType>[], context: GetActionsContext) => Action<ModuleType>[];
  context: GetActionsContext;
}

export default function useModuleAdminActions({ getActions, context }: UseModuleAdminActionsOptions) {
  const actions = useMemo<Action<ModuleType>[]>(() => {
    const actions: Action<ModuleType>[] = [
      {
        id: 'edit',
        label: ACMS.i18n('module_index.action.edit'),
        getHref: (data) =>
          ACMS.Library.acmsLink({
            bid: data.blog_id,
            admin: 'module_edit',
            searchParams: new URLSearchParams({
              mid: data.id.toString(),
              edit: 'update',
              ...(context.searchParams.has('rid') && { rid: context.searchParams.get('rid') as string }),
            }),
          }),
        type: 'primary',
        linkProps: {
          className: 'acms-admin-btn-admin',
        },
        condition: (data) => data.actions.includes('edit'),
      },
    ];
    // コア定義 → レジストリ（ctx.dataview.registerAction('module', ...)） → レガシー getter の順にマージする
    const registryActions = Object.values(getDataviewActionRegistry().getAll('module')) as Action<ModuleType>[];
    const combinedActions = [...actions, ...registryActions];
    return getActions(combinedActions, context);
  }, [getActions, context]);

  return actions;
}
