"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.reactLiveConvertor = reactLiveConvertor;

const parser = require('@babel/parser');

const traverse = require('@babel/traverse').default;

const path = require('path');

function getFilename(originalFilePath) {
  const [fileName] = path.basename(originalFilePath).split('.');
  return fileName;
}

function reactLiveConvertor(source, originalFilePath) {
  const fileName = getFilename(originalFilePath);

  if (!source) {
    return '';
  }

  let importBlock = '';
  let docCode = '';
  const packages = new Set();
  const fileContent = source.replace(/[\s\r\n]*$/, '');
  const ast = parser.parse(fileContent, {
    sourceType: 'module',
    plugins: ['jsx', 'classProperties']
  }); // Traverse the AST and find the import and export blocks

  let remainingCode = ast.program.body.filter(node => node.type !== 'ImportDeclaration').map(node => fileContent.substring(node.start, node.end)).join('').replace(/export default/, '').replace(/export /, '').trim();
  traverse(ast, {
    ImportDeclaration(path) {
      importBlock += `${fileContent.slice(path.node.start, path.node.end)}\n`;
      path.node.specifiers.forEach(specifier => {
        if (specifier.type === 'ImportSpecifier') {
          packages.add(specifier.imported.name);
        }

        if (specifier.local.type === 'Identifier') {
          packages.add(specifier.local.name);
        }
      });
    },

    ExpressionStatement(path) {
      const expression = path.get('expression');
      const expressionLeft = expression.get('left');

      if (path.isExpressionStatement() && expression.isAssignmentExpression() && expressionLeft.isMemberExpression()) {
        const docCheck = expressionLeft.toString();

        if (docCheck === `${fileName}.docs`) {
          docCode = expression.toString();
        }
      }
    } // IfStatement(path){
    //   if(path.node.type == 'IfStatement'){
    //     if(path.node.test.name == '__DOCS__'){
    //       let final = fileContent.slice(path.node.start,path.node.end)
    //       fileContent = fileContent.replace(final,'')
    //       console.log(fileContent);
    //     }
    //   }
    // }


  });
  remainingCode = remainingCode.replace(/__DOCS__/, true);
  remainingCode = remainingCode.replace(/`/g, '\\`').replace(/\$\{/g, '$\\{');
  const brcklets = `(function () { 

      ${remainingCode}  

      return <${fileName} />

    }) ()`;
  const addBractick = `\`${brcklets}\``;
  const template = `
  
    ${importBlock}
  
    import { LiveProvider, LiveEditor, LiveError, LivePreview } from '@zohodesk-private/react-live/es/index'
  
    export default class ${fileName} extends React.Component{
      render(){
        return(
          <LiveProvider scope={{${Array.from(packages).join(', ')}}} code={${addBractick}}>
         <LiveEditor   />
         <LiveError />
         <LivePreview    />
     
          </LiveProvider>
        )
      }
    }
  
    ${docCode}
    `;
  return template;
}