// This file has been automatically migrated to valid ESM format by Storybook.
import { createRequire } from 'node:module';
import { dirname, join, resolve } from 'path';

import type { StorybookConfig } from '@storybook/react-vite';
import { configureSort } from 'storybook-multilevel-sort';

const require = createRequire(import.meta.url);

configureSort({
    storyOrder: {
        welcome: null,
        changelog: null,
        playground: null,
        zeroheight: null,
        guides: {
            installation: null,
            'token explorer': null,
            'semantic tokens examples': null,
            'responsive props': null,
            'shorthand props': null,
            'custom components': null,
            assets: null,
            migrations: null,
        },
        intlformatted: null,
        utilities: null,
        components: null,
    },
    /**
     * Disable sorting on component level and display the stories as they are defined in the code.
     */
    compareNames: () => 0,
});

function getAbsolutePath(value) {
    return dirname(require.resolve(join(value, 'package.json')));
}

const config: StorybookConfig = {
    stories: [
        '../pages/**/*.@(stories.tsx|mdx)',
        '../../../packages/web-lib/src/**/*.stories.tsx',
        '../../../packages/web-lib/src/**/*.mdx',
    ],
    staticDirs: ['../public'],
    addons: [
        getAbsolutePath('@storybook/addon-a11y'),
        getAbsolutePath('@storybook/addon-links'),
        getAbsolutePath('@storybook/addon-vitest'),
        getAbsolutePath('@storybook/addon-docs'),
        getAbsolutePath('@chromatic-com/storybook'),
        getAbsolutePath('storybook-addon-pseudo-states'),
    ],
    framework: {
        name: getAbsolutePath('@storybook/react-vite'),
        options: {},
    },
    viteFinal: async config => {
        config.resolve = {
            ...config.resolve,
            dedupe: ['react', 'react-dom'],
        };
        return config;
    },

    typescript: {
        reactDocgen: 'react-docgen-typescript',
        reactDocgenTypescriptOptions: {
            shouldExtractLiteralValuesFromEnum: true,
            shouldExtractValuesFromUnion: true,
            shouldRemoveUndefinedFromOptional: true,
            include: [join(resolve('../../packages/web-lib'), 'src/components/**/*.tsx')],
        },
    },
};

export default config;
