const fs   = require( 'fs' );
const path = require( 'path' );
const glob = require( 'glob' );

export const rename = () => {
    const { join } = path;

    const blockStyleFiles = glob.sync(
        join( process.cwd(), 'build/blocks', '**', 'style-script.css' ),
    );

    if ( blockStyleFiles ) {
        blockStyleFiles.forEach( blockStyleFile => {
            fs.rename( blockStyleFile, blockStyleFile.replace( '-script.css', '.css' ), error => {
                if ( error ) {
                    console.log( error );
                }
            } );
        } );
    }

    const blockEditorStyleFiles = glob.sync(
        join( process.cwd(), 'build/blocks', '**', 'index.css' ),
    );

    if ( blockEditorStyleFiles ) {
        blockEditorStyleFiles.forEach( blockEditorStyleFile => {
            fs.rename( blockEditorStyleFile, blockEditorStyleFile.replace( 'index.css', 'editor.css' ), error => {
                if ( error ) {
                    console.log( error );
                }
            } );
        } );
    }

    const blockJsonFiles = glob.sync(
        join( process.cwd(), 'build/blocks', '**', 'block.json' ),
    );

    if ( blockJsonFiles ) {
        blockJsonFiles.forEach( filePath => {
            let blockJson = require( filePath );

            if ( blockJson?.editorScript ) {
                blockJson.editorScript = blockJson.editorScript.replace( '.tsx', '.js' );
            }

            if ( blockJson?.script ) {
                blockJson.script = blockJson.script.replace( '.tsx', '.js' );
            }

            if ( blockJson?.viewScript ) {
                blockJson.viewScript = blockJson.viewScript.replace( '.tsx', '.js' );
            }

            if ( blockJson?.editorStyle ) {
                blockJson.editorStyle = blockJson.editorStyle.replace( '.scss', '.css' );
            }

            if ( blockJson?.style ) {
                blockJson.style = blockJson.style.replace( '.scss', '.css' );
            }

            fs.writeFile( filePath, JSON.stringify( blockJson, null, 2 ), function writeJSON( error ) {
                if ( error ) {
                    return console.log( error );
                }
            } );
        } );
    }
};

export default rename;
