/* eslint-disable flowtype/require-valid-file-annotation */
import initStoryshots from '@storybook/addon-storyshots';
import { imageSnapshot } from '@storybook/addon-storyshots-puppeteer';
import path from 'path';

const PAGE_LOAD_TIMEOUT_MS = 200;
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

initStoryshots({
  suite: 'Image storyshots',
  storyKindRegex: /^((?!.*?A MANUAL).)*$/,
  test: imageSnapshot({
    storybookUrl: `file://${process.cwd()}/storybook-static`,
    beforeScreenshot: () => sleep(PAGE_LOAD_TIMEOUT_MS),
    getScreenshotOptions: () => ({
      omitBackground: true,
    }),
    getMatchOptions: ({ context: { kind } }) => ({
      failureThreshold: 0.01, // 1% threshold,
      failureThresholdType: 'percent',
      customSnapshotsDir: path.join(
        __dirname,
        '__image_snapshots__',
        ...kind.split('/'),
      ),
      customDiffDir: path.join(
        __dirname,
        '__image_snapshots__/__diff_output__',
      ),
    }),
  }),
});
