from abstract_webserver import *
from .utils import *
from .import_utils import *
from .replace_utils import *
def get_general_layout(item_paths=None,excluded_exts=None,server_dir=None,local_server_dir=None,include_site_view=False):
    excluded_exts = excluded_exts or []
    item_paths = [str(item_path) for item_path in item_paths]
    server_paths = bulk_convert_to_server_docs(item_paths)
    contents = ['']
    paths = ['']
    for i,item_path in enumerate(item_paths):
        item_path = str(item_path)
        server_path = server_paths[i]
        path_only=False
        for key in excluded_exts:
            if server_path and server_path.endswith(key):
                path_only=True
                paths.append(server_path)
                break
        if not path_only:
            data = get_doc_content(item_path)
            if data:
                contents.append(data)
    paths = '\n'.join(paths)
    contents = '\n'.join(contents)
    return paths+contents
def get_imports_and_content(react_file,tsconfig_path=None,excluded_exts=None,desired_exts=None,server_dir=None,local_server_dir=None,include_site_view=False):
    src_dir = find_src_dir(react_file)
    server_mgr = serverManager(src_dir=src_dir)
    tsconfig_path = tsconfig_path or glob_search(server_mgr.main_dir,'tsconfig',ext='.json')
    if tsconfig_path and isinstance(tsconfig_path,list):
        tsconfig_path = tsconfig_path[0]
    desired_exts = desired_exts or []
    excluded_exts = excluded_exts or []
    item_paths = set()
    react_files = make_list(react_file)
    for react_file in react_files:
        if os.path.isdir(react_file):
            sub_react_files =glob_search(directory,basename,ext=None)
            for sub_react_file in sub_react_files:
                item_paths = get_imports(sub_react_file,tsconfig_path=tsconfig_path,seen=item_paths)
        else:
            item_paths = get_imports(react_file,tsconfig_path=tsconfig_path,seen=item_paths)
    if include_site_view:
        docs = [os.path.join(server_mgr.main_dir,item) for item in os.listdir(server_mgr.main_dir)]
        item_paths = item_paths+docs
    general_layout = get_general_layout(item_paths=item_paths,excluded_exts=excluded_exts,include_site_view=include_site_view)
    return general_layout
server_dir='/var/www/webserver/clownworld/bolshevid'
local_server_dir='/home/computron/mnt/webserver/clownworld/bolshevid'
react_file = '/home/computron/mnt/webserver/clownworld/bolshevid/src/components/BolshevidVideo/BolshevidVideo.tsx'
tsconfig_path = '/home/computron/mnt/webserver/clownworld/bolshevid/tsconfig.json'
excluded_exts=['.ts','.js','.css']
desired_exts=['.tsx']
output_contents = get_imports_and_content(react_file=react_file,
                              tsconfig_path=tsconfig_path,
                              excluded_exts=excluded_exts,
                              desired_exts=desired_exts,
                              local_server_dir=local_server_dir,
                              server_dir=server_dir,include_site_view=True)
# example usage:
paths = [
    "/home/computron/Downloads/VideoDetails(2).ts",
    "/home/computron/Downloads/VideoPlayer(2).ts",
    "/home/computron/Downloads/MiniPlayer.ts",
]
react_src_dir = "/home/computron/mnt/webserver/clownworld/bolshevid/src"
backup_dir = '/home/computron/mnt/webserver/clownworld/bolshevid/backups'
replace_react_files(src_dir=react_src_dir,
                    paths=paths,
                    backup_dir=backup_dir)


