const path = require('path')

// copied from jest internals, to support default functionality and
// to keep existing snapshots in their current location. We can remove this
// as we migrate away from the centralized __tests__ location.
function defaultResolveSnapshotPath(testPath, snapshotExtension) {
  return path.join(
    path.join(path.dirname(testPath), '__snapshots__'),
    path.basename(testPath) + snapshotExtension
  )
}

function defaultResolveTestPath(snapshotPath, snapshotExtension) {
  return path.resolve(
    path.dirname(snapshotPath),
    '..',
    path.basename(snapshotPath, snapshotExtension)
  )
}

// helper to check if we should use default resolver functionality
function isLegacyDirectory(path) {
  return (
    path.includes('src/components/__tests__') ||
    path.includes('src/layouts/__tests__')
  )
}

module.exports = {
  testPathForConsistencyCheck: '.jest/example.test.js',

  resolveSnapshotPath(testPath, snapshotExtension) {
    if (isLegacyDirectory(testPath)) {
      return defaultResolveSnapshotPath(testPath, snapshotExtension)
    }

    return testPath + snapshotExtension
  },

  resolveTestPath(snapshotFilePath, snapshotExtension) {
    if (isLegacyDirectory(snapshotFilePath)) {
      return defaultResolveTestPath(snapshotFilePath, snapshotExtension)
    }

    return snapshotFilePath.slice(0, -snapshotExtension.length)
  },
}
