import path from 'path';
import fs from 'fs-extra';
import findUp from 'find-up';
import resolveFrom from 'resolve-from';
import fetch from 'node-fetch';
import deprecate from 'util-deprecate';
import dedent from 'ts-dedent';

import { logger } from '@storybook/node-logger';

import loadPresets from '../presets';
import loadCustomPresets from '../common/custom-presets';
import { typeScriptDefaults } from '../config/defaults';

const getAutoRefs = async (options) => {
  const location = await findUp('package.json', { cwd: options.configDir });
  const directory = path.dirname(location);

  const { dependencies, devDependencies } = await fs.readJSON(location);

  const list = await Promise.all(
    Object.keys({ ...dependencies, ...devDependencies }).map(async (d) => {
      try {
        const l = resolveFrom(directory, path.join(d, 'package.json'));

        const { storybook, name, version } = await fs.readJSON(l);

        if (storybook?.url) {
          return { id: name, ...storybook, version };
        }
      } catch {
        logger.warn(`unable to find package.json for ${d}`);
        return undefined;
      }
      return undefined;
    })
  );

  return list.filter(Boolean);
};

const checkRef = (url) =>
  fetch(`${url}/iframe.html`).then(
    ({ ok }) => ok,
    () => false
  );

const stripTrailingSlash = (url) => url.replace(/\/$/, '');

const toTitle = (input) => {
  const result = input
    .replace(/[A-Z]/g, (f) => ` ${f}`)
    .replace(/[-_][A-Z]/gi, (f) => ` ${f.toUpperCase()}`)
    .replace(/-/g, ' ')
    .replace(/_/g, ' ');

  return `${result.substring(0, 1).toUpperCase()}${result.substring(1)}`.trim();
};

const deprecatedDefinedRefDisabled = deprecate(
  () => {},
  dedent`
    Deprecated parameter: disabled => disable

    https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-package-composition-disabled-parameter
  `
);

async function getManagerWebpackConfig(options, presets) {
  const typescriptOptions = await presets.apply('typescript', { ...typeScriptDefaults }, options);
  const babelOptions = await presets.apply('babel', {}, { ...options, typescriptOptions });

  const autoRefs = await getAutoRefs(options);
  const definedRefs = await presets.apply('refs', undefined, options);
  const entries = await presets.apply('managerEntries', [], options);

  const refs = {};

  if (autoRefs && autoRefs.length) {
    autoRefs.forEach(({ id, url, title, version }) => {
      refs[id.toLowerCase()] = {
        id: id.toLowerCase(),
        url: stripTrailingSlash(url),
        title,
        version,
      };
    });
  }

  if (definedRefs) {
    Object.entries(definedRefs).forEach(([key, value]) => {
      const { disable, disabled } = value;

      if (disable || disabled) {
        if (disabled) {
          deprecatedDefinedRefDisabled();
        }

        delete refs[key.toLowerCase()];
        return;
      }

      const url = typeof value === 'string' ? value : value.url;
      const rest =
        typeof value === 'string'
          ? { title: toTitle(key) }
          : { ...value, title: value.title || toTitle(value.key || key) };

      refs[key.toLowerCase()] = {
        id: key.toLowerCase(),
        ...rest,
        url: stripTrailingSlash(url),
      };
    });
  }

  if (autoRefs || definedRefs) {
    entries.push(path.resolve(path.join(options.configDir, `generated-refs.js`)));

    // verify the refs are publicly reachable, if they are not we'll require stories.json at runtime, otherwise the ref won't work
    await Promise.all(
      Object.entries(refs).map(async ([k, value]) => {
        const ok = await checkRef(value.url);

        refs[k] = { ...value, type: ok ? 'server-checked' : 'unknown' };
      })
    );
  }

  return presets.apply('managerWebpack', {}, { ...options, babelOptions, entries, refs });
}

export default async (options) => {
  const { corePresets = [], frameworkPresets = [], overridePresets = [], ...restOptions } = options;

  const presetsConfig = [
    ...corePresets,
    require.resolve('../common/babel-cache-preset.js'),
    ...frameworkPresets,
    ...loadCustomPresets(options),
    ...overridePresets,
  ];

  const presets = loadPresets(presetsConfig, restOptions);

  return getManagerWebpackConfig({ ...restOptions, presets }, presets);
};

@@ -450,32 +450,46 @@
 = async (options
+, disabledRefs
 ) =%3E %7B%0A  const l
@@ -674,25 +674,24 @@
 ation);%0A
-%0A
   const 
 list = a
@@ -686,37 +686,14 @@
 nst 
-list = await Promise.all(%0A   
+deps =
  Obj
@@ -742,16 +742,114 @@
 cies %7D).
+filter(%0A    (dep) =%3E !disabledRefs.includes(dep)%0A  );%0A%0A  const list = await Promise.all(%0A    deps.
 map(asyn
@@ -2236,55 +2236,8 @@
 );%0A%0A
-  const autoRefs = await getAutoRefs(options);%0A
   co
@@ -2295,32 +2295,503 @@
 ined, options);%0A
+  const disabledRefs = Object.entries(definedRefs)%0A    .filter((%5Bkey, value%5D) =%3E %7B%0A      const %7B disable, disabled %7D = value;%0A%0A      if (disable %7C%7C disabled) %7B%0A        if (disabled) %7B%0A          deprecatedDefinedRefDisabled();%0A        %7D%0A%0A        delete definedRefs%5Bkey%5D; // Also delete the ref that is disabled in definedRefs%0A%0A        return true;%0A      %7D%0A%0A      return false;%0A    %7D)%0A    .map((ref) =%3E ref%5B0%5D);%0A  const autoRefs = await getAutoRefs(options, disabledRefs);%0A
   const entries 
@@ -3130,24 +3130,24 @@
 inedRefs) %7B%0A
+
     Object.e
@@ -3198,227 +3198,8 @@
 %3E %7B%0A
-      const %7B disable, disabled %7D = value;%0A%0A      if (disable %7C%7C disabled) %7B%0A        if (disabled) %7B%0A          deprecatedDefinedRefDisabled();%0A        %7D%0A%0A        delete refs%5Bkey.toLowerCase()%5D;%0A        return;%0A      %7D%0A%0A
     

Update manager-config.js