import type { DevToolbarRule, DevToolbarRuleContext } from "./types.ts";
import type { DevToolbarIssue } from "../types.ts";
import { accessibilityRules } from "./accessibility.ts";
import { seoRules } from "./seo.ts";
import { imageRules } from "./images.ts";
import { mediaRules } from "./media.ts";
import { colorRules } from "./color.ts";
import { htmlRules } from "./html.ts";
import { formRules } from "./forms.ts";
import { linkRules } from "./links.ts";
import { performanceRules } from "./performance.ts";
import { responsiveRules } from "./responsive.ts";
import { securityRules } from "./security.ts";

export * from "./types.ts";
export * from "./helpers.ts";
export {
  accessibilityRules,
  seoRules,
  imageRules,
  mediaRules,
  colorRules,
  htmlRules,
  formRules,
  linkRules,
  performanceRules,
  responsiveRules,
  securityRules,
};

export const DEV_TOOLBAR_RULES: DevToolbarRule[] = [
  ...accessibilityRules,
  ...seoRules,
  ...imageRules,
  ...mediaRules,
  ...colorRules,
  ...htmlRules,
  ...formRules,
  ...linkRules,
  ...performanceRules,
  ...responsiveRules,
  ...securityRules,
];

export async function runDevToolbarRules(
  context: DevToolbarRuleContext,
  rules = DEV_TOOLBAR_RULES,
): Promise<DevToolbarIssue[]> {
  const settled = await Promise.all(
    rules.map(async (rule) => {
      try {
        return await rule.run(context);
      } catch (error) {
        console.warn(`[WRNexus DevToolbar] Rule ${rule.id} failed`, error);
        return [];
      }
    }),
  );
  const unique = new Map<string, DevToolbarIssue>();
  for (const issue of settled.flat()) unique.set(issue.fingerprint, issue);
  return [...unique.values()];
}
