import * as path from 'path' ; import * as fs from 'fs' ; import * as FlrConstant from '../FlrConstant' ; export class FlrCodeUtil { public static generate_R_class ( packageName ) { let code = `// IT IS GENERATED BY FLR - DO NOT MODIFY BY HAND
        // YOU CAN GET MORE DETAILS ABOUT FLR FROM:
        // - https://github.com/Fly-Mix/flr-cli
        // - https://github.com/Fly-Mix/flr-vscode-extension
        // - https://github.com/Fly-Mix/flr-as-plugin
        //
        
        // ignore: unused_import
        import 'package:flutter/widgets.dart';
        // ignore: unused_import
        import 'package:flutter/services.dart' show rootBundle;
        // ignore: unused_import
        import 'package:path/path.dart' as path;
        // ignore: unused_import
        import 'package:flutter_svg/flutter_svg.dart';
        // ignore: unused_import
        import 'package:r_dart_library/asset_svg.dart';
        
        /// This \`R\` class is generated and contains references to static asset resources.
        class R {
          /// package name: ${ packageName }
          static const package = "${ packageName }";
        
          /// This \`R.image\` struct is generated, and contains static references to static non-svg type image asset resources.
          static const image = _R_Image();
        
          /// This \`R.svg\` struct is generated, and contains static references to static svg type image asset resources.
          static const svg = _R_Svg();
        
          /// This \`R.text\` struct is generated, and contains static references to static text asset resources.
          static const text = _R_Text();
        
          /// This \`R.fontFamily\` struct is generated, and contains static references to static font asset resources.
          static const fontFamily = _R_FontFamily();
        }` ; return code ; } public static generate_AssetResource_class ( packageName ) { let code = `/// Asset resource’s metadata class.
      /// For example, here is the metadata of \`packages/flutter_demo/assets/images/example.png\` asset:
      /// - packageName：flutter_demo
      /// - assetName：assets/images/example.png
      /// - fileDirname：assets/images
      /// - fileBasename：example.png
      /// - fileBasenameNoExtension：example
      /// - fileExtname：.png
      class AssetResource {
        /// Creates an object to hold the asset resource’s metadata.
        const AssetResource(this.assetName, {this.packageName});
      
        /// The name of the main asset from the set of asset resources to choose from.
        final String assetName;
      
        /// The name of the package from which the asset resource is included.
        final String? packageName;
      
        /// The name used to generate the key to obtain the asset resource. For local assets
        /// this is [assetName], and for assets from packages the [assetName] is
        /// prefixed 'packages/<package_name>/'.
        String get keyName => packageName == null ? assetName : "packages/$packageName/$assetName";
      
        /// The file basename of the asset resource.
        String get fileBasename {
          final basename = path.basename(assetName);
          return basename;
        }
      
        /// The no extension file basename of the asset resource.
        String get fileBasenameNoExtension {
          final basenameWithoutExtension = path.basenameWithoutExtension(assetName);
          return basenameWithoutExtension;
        }
      
        /// The file extension name of the asset resource.
        String get fileExtname {
          final extension = path.extension(assetName);
          return extension;
        }
      
        /// The directory path name of the asset resource.
        String get fileDirname {
          var dirname = assetName;
          if (packageName != null) {
            final packageStr = "packages/$packageName/";
            dirname = dirname.replaceAll(packageStr, "");
          }
          final filenameStr = "$fileBasename/";
          dirname = dirname.replaceAll(filenameStr, "");
          return dirname;
        }
      }` ; return code ; } public static generateAssetId ( asset , usedAssetIdArray , priorAssetType = '.*' ) { let fileExtName = path . extname ( asset ) ; let fileBasenameWithoutExtension = path . parse ( asset ) . name ; var assetId = fileBasenameWithoutExtension ; if ( priorAssetType === '.*' || fileExtName !== priorAssetType ) { let extInfo = '_' + fileExtName . substring ( 1 ) ; assetId = fileBasenameWithoutExtension + extInfo ; } assetId = assetId . replace ( /[^a-zA-Z0-9_$]/g , '_' ) ; let capital = assetId . charAt ( 0 ) . toLowerCase ( ) ; assetId = capital + assetId . substring ( 1 ) ; let capitalRegex = /[0-9_$]/ ; if ( capitalRegex . test ( capital ) ) { assetId = 'a' + assetId ; } if ( usedAssetIdArray !== null && usedAssetIdArray . includes ( assetId ) ) { var repeatCount = 1 ; let idBrotherRegex = new RegExp ( `^${ assetId }\\$[1-9][0-9]*$` ) ; let curAssetIdBrothers = usedAssetIdArray . filter ( ( usedAssetId ) => idBrotherRegex . test ( usedAssetId ) ) ; if ( curAssetIdBrothers !== null ) { repeatCount += curAssetIdBrothers . length ; } assetId = `${ assetId }$${ repeatCount }` ; } return assetId ; } public static generateAssetComment ( asset , packageName ) { let packagesPrefix = 'packages/' + packageName + '/' ; if ( asset . startsWith ( packagesPrefix ) ) { let assetName = asset . replace ( packagesPrefix , '' ) ; let assetComment = 'asset: lib/' + assetName ; return assetComment ; } else { let assetName = asset ; let assetComment = 'asset: ' + assetName ; return assetComment ; } } public static generate_AssetResource_property ( asset , assetIdDict , packageName , isPackageProjectType , priorAssetType = '.*' ) { let assetId = assetIdDict . get ( asset ) ; let assetComment = this . generateAssetComment ( asset , packageName ) ; var assetName = '' ; var needPackage = false ; let packagesPrefix = 'packages/' + packageName + '/' ; if ( asset . startsWith ( packagesPrefix ) ) { assetName = asset . replace ( packagesPrefix , '' ) ; needPackage = true ; } else { assetName = asset ; if ( isPackageProjectType ) { needPackage = true ; } else { needPackage = false ; } } let escapedAssetName = assetName . replace ( /[$]/g , '\\$' ) ; if ( needPackage ) { let code = `  /// ${ assetComment }
      // ignore: non_constant_identifier_names
      final ${ assetId } = const AssetResource("${ escapedAssetName }", packageName: R.package);` ; return code ; } else { let code = `  /// ${ assetComment }
      // ignore: non_constant_identifier_names
      final ${ assetId } = const AssetResource("${ escapedAssetName }", packageName: null);` ; return code ; } } public static generate__R_Image_AssetResource_class ( nonSvgImageAssetArray , nonSvgImageAssetIdDict , packageName , isPackageProjectType ) { var all_g_AssetResource_property_code = '' ; nonSvgImageAssetArray . forEach ( ( asset ) => { all_g_AssetResource_property_code += '\n' ; let g_AssetResource_property_code = this . generate_AssetResource_property ( asset , nonSvgImageAssetIdDict , packageName , isPackageProjectType , FlrConstant . PRIOR_NON_SVG_IMAGE_FILE_TYPE ) ; all_g_AssetResource_property_code += g_AssetResource_property_code ; } ) ; let code = `// ignore: camel_case_types
      class _R_Image_AssetResource {
        const _R_Image_AssetResource();
      ${ all_g_AssetResource_property_code }
      }` ; return code ; } public static generate__R_Svg_AssetResource_class ( svgImageAssetArray , svgImageAssetIdDict , packageName , isPackageProjectType ) { var all_g_AssetResource_property_code = '' ; svgImageAssetArray . forEach ( ( asset ) => { all_g_AssetResource_property_code += '\n' ; let g_AssetResource_property_code = this . generate_AssetResource_property ( asset , svgImageAssetIdDict , packageName , isPackageProjectType , FlrConstant . PRIOR_SVG_IMAGE_FILE_TYPE ) ; all_g_AssetResource_property_code += g_AssetResource_property_code ; } ) ; let code = `// ignore: camel_case_types
    class _R_Svg_AssetResource {
      const _R_Svg_AssetResource();
    ${ all_g_AssetResource_property_code }
    }` ; return code ; } public static generate__R_Text_AssetResource_class ( textAssetArray , textAssetIdDict , packageName , isPackageProjectType ) { var all_g_AssetResource_property_code = '' ; textAssetArray . forEach ( ( asset ) => { all_g_AssetResource_property_code += '\n' ; let g_AssetResource_property_code = this . generate_AssetResource_property ( asset , textAssetIdDict , packageName , isPackageProjectType , FlrConstant . PRIOR_NON_SVG_IMAGE_FILE_TYPE ) ; all_g_AssetResource_property_code += g_AssetResource_property_code ; } ) ; let code = `// ignore: camel_case_types
    class _R_Text_AssetResource {
      const _R_Text_AssetResource();
    ${ all_g_AssetResource_property_code }
    }` ; return code ; } public static generate__R_Image_class ( nonSvgImageAssetArray , nonSvgImageAssetIdDict , packageName ) { var all_g_Asset_method_code = '' ; nonSvgImageAssetArray . forEach ( ( asset ) => { all_g_Asset_method_code += '\n' ; let assetId = nonSvgImageAssetIdDict . get ( asset ) ; let assetComment = this . generateAssetComment ( asset , packageName ) ; let g_Asset_method_code = `  /// ${ assetComment }
      // ignore: non_constant_identifier_names
      AssetImage ${ assetId }() {
        return AssetImage(asset.${ assetId }.keyName);
      }` ; all_g_Asset_method_code += g_Asset_method_code ; } ) ; let code = `/// This \`_R_Image\` class is generated and contains references to static non-svg type image asset resources.
    // ignore: camel_case_types
    class _R_Image {
      const _R_Image();
    
      final asset = const _R_Image_AssetResource();
    ${ all_g_Asset_method_code }
    }` ; return code ; } public static generate__R_Svg_class ( svgImageAssetArray , svgImageAssetIdDict , packageName ) { var all_g_Asset_method_code = '' ; svgImageAssetArray . forEach ( ( asset ) => { all_g_Asset_method_code += '\n' ; let assetId = svgImageAssetIdDict . get ( asset ) ; let assetComment = this . generateAssetComment ( asset , packageName ) ; let g_Asset_method_code = `  /// ${ assetComment }
      // ignore: non_constant_identifier_names
      AssetSvg ${ assetId }({required double width, required double height}) {
        final imageProvider = AssetSvg(asset.${ assetId }.keyName, width: width, height: height);
        return imageProvider;
      }` ; all_g_Asset_method_code += g_Asset_method_code ; } ) ; let code = `/// This \`_R_Svg\` class is generated and contains references to static svg type image asset resources.
    // ignore: camel_case_types
    class _R_Svg {
      const _R_Svg();
    
      final asset = const _R_Svg_AssetResource();
    ${ all_g_Asset_method_code }
    }` ; return code ; } public static generate__R_Text_class ( textAssetArray , textAssetIdDict , packageName ) { var all_g_Asset_method_code = '' ; textAssetArray . forEach ( ( asset ) => { all_g_Asset_method_code += '\n' ; let assetId = textAssetIdDict . get ( asset ) ; let assetComment = this . generateAssetComment ( asset , packageName ) ; let g_Asset_method_code = `  /// ${ assetComment }
      // ignore: non_constant_identifier_names
      Future<String> ${ assetId }() {
        final str = rootBundle.loadString(asset.${ assetId }.keyName);
        return str;
      }` ; all_g_Asset_method_code += g_Asset_method_code ; } ) ; let code = `/// This \`_R_Text\` class is generated and contains references to static text asset resources.
    // ignore: camel_case_types
    class _R_Text {
      const _R_Text();
    
      final asset = const _R_Text_AssetResource();
    ${ all_g_Asset_method_code }
    }` ; return code ; } public static generateFontFamilyId ( fontFamilyName ) { var fontFamilyId = fontFamilyName ; fontFamilyId = fontFamilyId . replace ( /[^a-zA-Z0-9_$]/g , '_' ) ; let capital = fontFamilyId . charAt ( 0 ) . toLowerCase ( ) ; fontFamilyId = capital + fontFamilyId . substring ( 1 ) ; let capitalRegex = /[0-9_$]/ ; if ( capitalRegex . test ( capital ) ) { fontFamilyId = 'a' + fontFamilyId ; } return fontFamilyId ; } public static generate__R_FontFamily_class ( fontFamilyConfigArray , packageName ) { var all_g_AssetResource_property_code = '' ; fontFamilyConfigArray . forEach ( ( fontFamilyConfig ) => { all_g_AssetResource_property_code += '\n' ; let fontFamilyName = fontFamilyConfig [ 'family' ] ; if ( fontFamilyName === undefined ) { return ; } let fontFamilyId = this . generateFontFamilyId ( fontFamilyName ) ; let fontFamilyComment = `font family: ${ fontFamilyName }` ; let g_AssetResource_property_code = `  /// ${ fontFamilyComment }
      // ignore: non_constant_identifier_names
      final ${ fontFamilyId } = "${ fontFamilyName }";` ; all_g_AssetResource_property_code += g_AssetResource_property_code ; } ) ; let code = `/// This \`_R_FontFamily\` class is generated and contains references to static font asset resources.
    // ignore: camel_case_types
    class _R_FontFamily {
      const _R_FontFamily();
    ${ all_g_AssetResource_property_code }
    }` ; return code ; } }