const path = require('path');
let rootPath = path.join(__dirname, '\\../../../');

module.exports = {
  stories: [
    '../src/**/**/**/*.stories.js',
    '../src/**/**/**/**/*.stories.js',
    '../src/**/**/**/**/**/*.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/organisms/navigation/navigation.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/organisms/anchor/template-one/anchor.stories',
    '../../node_modules/gatsby-core-theme/src/components/organisms/anchor/template-two/template-two.stories',
    '../../node_modules/gatsby-core-theme/src/components/molecules/bonus/template-one/bonus.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/molecules/bonus/template-two/bonus.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/molecules/content/text.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/molecules/content/show-more.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/molecules/content/content.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/molecules/content/lists/lists.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/molecules/content/frame/frame.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/molecules/content/table/table-one.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/molecules/content/table/table-two.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/molecules/pagination/pagination.stories.js',
    '../../node_modules/gatsby-core-theme/src/components/organisms/carousel/template-one/template-one.stories',
    '../../node_modules/gatsby-core-theme/src/components/organisms/carousel/template-two/template-two.stories',
    '../../node_modules/gatsby-core-theme/src/components/molecules/spotlights_v2/icon/template-one',
    '../../node_modules/gatsby-core-theme/src/components/molecules/spotlights_v2/image/template-one/template-one.stories',
    '../../node_modules/gatsby-core-theme/src/components/molecules/spotlights_v2/image/template-two/template-two.stories',
    '../../node_modules/gatsby-core-theme/src/components/molecules/spotlights_v2/image-text/template-one/template-one.stories',
    '../../node_modules/gatsby-core-theme/src/components/molecules/spotlights_v2/image-text/template-two/template-two.stories',
    '../../node_modules/gatsby-core-theme/src/components/molecules/spotlights_v2/image-text/template-three/template-three.stories',
  ],
  addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
  env: (config) => ({
    ...config,
    IMAGE_CDN_URL: 'https://cdn.irishluck.ie',
    STORYBOOK_GATSBY_SITE_NAME: 'irishluck.ie',
    STORYBOOK_GATSBY_TRACKER_LINK_PREFIX: 'visit',
  }),
  core: {
    builder: 'webpack5',
  },
  webpackFinal: async (config) => {
    config.module.rules.push({
      test: /\.scss$/,
      use: [
        require.resolve('style-loader'),
        {
          loader: require.resolve('css-loader'),
          options: {
            importLoaders: 1,
            modules: true,
          },
        },
        require.resolve('sass-loader'),
        {
          loader: require.resolve('sass-resources-loader'),
          options: {
            resources: [
              path.resolve(rootPath, '../node_modules/gatsby-core-theme/src/styles/globals.scss'),
            ],
          },
        },
      ],
    });
    config.module.rules[0].exclude = [/node_modules\/(?!(gatsby)\/)(?!(gatsby-core-theme)\/)/];
    config.module.rules[0].use[0].loader = require.resolve('babel-loader');
    config.module.rules[0].use[0].options.presets = [
      require.resolve('@babel/preset-react'),
      require.resolve('@babel/preset-env'),
    ];
    config.module.rules[0].use[0].options.plugins.push(
      require.resolve('babel-plugin-remove-graphql-queries')
    );
    config.resolve.alias = {
      '~atoms': path.resolve(rootPath, '../node_modules/gatsby-core-theme/src/components/atoms/'),
      '~molecules': path.resolve(
        rootPath,
        '../node_modules/gatsby-core-theme/src/components/molecules'
      ),
      '~organisms': path.resolve(
        rootPath,
        '../node_modules/gatsby-core-theme/src/components/organisms'
      ),
      '~pages': path.resolve(rootPath, '../node_modules/gatsby-core-theme/src/components/pages'),
      '~helpers': path.resolve(rootPath, '../node_modules/gatsby-core-theme/src/helpers'),
      '~hooks': path.resolve(rootPath, '../node_modules/gatsby-core-theme/src/hooks'),
      '~tests': path.resolve(rootPath, '../node_modules/gatsby-core-theme/tests'),
      '~context': path.resolve(rootPath, '../node_modules/gatsby-core-theme/src/context'),
      'gatsby-page-utils': path.resolve(__dirname, '../../node_modules/gatsby-page-utils/dist'),
      'gatsby-core-theme/src/hooks/gatsby-img': path.resolve(
        __dirname,
        '../../node_modules/gatsby-core-theme/src/hooks/lazy-image'
      ),
      '../../../hooks/gatsby-img': path.resolve(
        __dirname,
        '../../node_modules/gatsby-core-theme/src/hooks/lazy-image'
      ),
    };
    config.resolve.alias['$virtual/lazy-client-sync-requires'] = path.resolve(
      `./__mocks__/virtual-lazy-client-sync-requires.js`
    );
    config.resolve.mainFields = ['browser', 'module', 'main'];
    return config;
  },
};
