import type { StorybookConfig } from '@storybook/react-vite';
import type { PluginOption } from 'vite';

const sbConfig: StorybookConfig = {
  "stories": [
    "../app/**/*.mdx",
    "../app/**/*.stories.@(js|jsx|mjs|ts|tsx)"
  ],
  "addons": [
    "@storybook/addon-vitest",
    "@storybook/addon-a11y",
    "@storybook/addon-docs",
    "storybook-addon-remix-react-router"
  ],
  "framework": "@storybook/react-vite",
  async viteFinal(config) {
    // React Router's Vite plugin throws if not used with a vite config file,
    // so we must remove it (and the Sentry wrapper that includes it) from the
    // Storybook build. reactRouter() returns an array of plugins, so we need
    // to flatten before filtering.
    const EXCLUDED_PREFIXES = ["react-router", "sentry-react-router"];

    function flatFilter(plugins: PluginOption[]): PluginOption[] {
      return plugins
        .filter((p) => p !== null && p !== undefined)
        .flatMap((p) => (Array.isArray(p) ? flatFilter(p) : [p]))
        .filter((p) => {
          if (typeof p === "object" && p !== null && "name" in p && typeof p.name === "string") {
            return !EXCLUDED_PREFIXES.some(
              (prefix) => p.name === prefix || (p.name as string).startsWith(prefix + ":")
            );
          }
          return true;
        });
    }

    config.plugins = flatFilter(config.plugins ?? []);
    return config;
  },
};
export default sbConfig;