const path = require('path');
const withStorybook = require('@storybook/react-native/metro/withStorybook');

const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const { wrapWithReanimatedMetroConfig } = require('react-native-reanimated/metro-config');

const defaultConfig = getDefaultConfig(__dirname);

const sourceExts = [...defaultConfig.resolver.sourceExts, 'mjs'];

const config = {
	transformer: {
		unstable_allowRequireContext: true
	},
	resolver: {
		// When running E2E tests, prioritize .mock.ts files for app code
		// Note: react-native-mmkv's internal mock file is disabled via patch-package
		sourceExts: process.env.RUNNING_E2E_TESTS === 'true' ? ['mock.ts', ...sourceExts] : sourceExts
	}
};

const finalConfig = wrapWithReanimatedMetroConfig(mergeConfig(getDefaultConfig(__dirname), config));

const storybookOptions = {
	// set to false to disable storybook specific settings
	// you can use a env variable to toggle this
	enabled: process.env.USE_STORYBOOK === 'true',
	// path to your storybook config folder
	configPath: path.resolve(__dirname, './.rnstorybook'),
	// set this to true to remove storybook from the bundle when disabled
	onDisabledRemoveStorybook: true
};

module.exports = withStorybook(finalConfig, storybookOptions);
