import { loadPreviewOrConfigFile } from '@storybook/core-common' ; import { normalizePath } from 'vite' ; import { listStories } from './list-stories' ; import type { ExtendedOptions } from './types' ; function replaceCJStoESMPath ( entryPath ) { return entryPath . replace ( '/cjs/' , '/esm/' ) ; } export async function generateIframeScriptCode ( options ) { const { presets , configDir , framework , frameworkPath } = options ; const previewEntries = ( await presets . apply ( 'previewEntries' , [ ] , options ) ) . map ( replaceCJStoESMPath ) ; const frameworkImportPath = frameworkPath || `@storybook/${ framework }` ; const previewOrConfigFile = loadPreviewOrConfigFile ( { configDir } ) ; const presetEntries = await presets . apply ( 'config' , [ ] , options ) ; const configEntries = [ ... presetEntries , previewOrConfigFile ] . filter ( Boolean ) ; const storyEntries = await listStories ( options ) ; const absoluteFilesToImport = ( files , name ) => files . map ( ( el , i ) => `import ${ name ? `* as ${ name }_${ i } from ` : '' }'/@fs/${ normalizePath ( el ) }'` ) . join ( '\n' ) ; const importArray = ( name , length ) => `[${ new Array ( length ) . fill ( 0 ) . map ( ( _ , i ) => `${ name }_${ i }` ) . join ( ',' ) }]` ; todo const code = `
    import { configure } from '${ frameworkImportPath }';
    /* ${ previewEntries . map ( ( entry ) => `// preview entry\nimport '${ entry }';` ) . join ( '\n' ) } */

    import {
      addDecorator,
      addParameters,
      addLoader,
      addArgTypesEnhancer,
      addArgsEnhancer
    } from '@storybook/client-api';
    import { logger } from '@storybook/client-logger';
    ${ absoluteFilesToImport ( configEntries , 'config' ) }
    ${ absoluteFilesToImport ( storyEntries , 'story' ) }
      
    const configs = ${ importArray ( 'config' , configEntries . length ) }
    configs.forEach(config => {
      Object.keys(config).forEach((key) => {
        const value = config[key];
        switch (key) {
          case 'args':
          case 'argTypes': {
            return logger.warn('Invalid args/argTypes in config, ignoring.', JSON.stringify(value));
          }
          case 'decorators': {
            return value.forEach((decorator) => addDecorator(decorator, false));
          }
          case 'loaders': {
            return value.forEach((loader) => addLoader(loader, false));
          }
          case 'parameters': {
            return addParameters({ ...value }, false);
          }
          case 'argTypesEnhancers': {
            return value.forEach((enhancer) => addArgTypesEnhancer(enhancer));
          }
          case 'argsEnhancers': {
            return value.forEach((enhancer) => addArgsEnhancer(enhancer))
          }
          case 'globals':
          case 'globalTypes': {
            const v = {};
            v[key] = value;
            return addParameters(v, false);
          }
          case 'decorateStory':
          case 'renderToDOM': {
            return null; // This key is not handled directly in v6 mode.
          }
          default: {
            // eslint-disable-next-line prefer-template
            return console.log(key + ' was not supported :( !');
          }
        }
      });
    })
    
    /* TODO: not quite sure what to do with this, to fix HMR
    if (import.meta.hot) {
        import.meta.hot.accept();    
    }
    */
    
    configure(() => ${ importArray ( 'story' , storyEntries . length ) }.filter(el => el.default), { hot: import.meta.hot }, false); // not sure if the import.meta.hot thing is correct
    ` . trim ( ) ; return code ; }