import {CLEAR_HISTORY_COMMAND} from 'lexical';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {fileHelper} from './fileHelper'

export function importFile(pathName) {
    readTextFileFromSystem(pathName,(text) => {
        const [editor]=useLexicalComposerContext()
        const json = JSON.parse(text);
        const editorState = editor.parseEditorState(
            JSON.stringify(json.editorState),
        );
        editor.setEditorState(editorState);
        editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
    });
}

async function readTextFileFromSystem(pathName, callback) {
    console.log("fileHelper",fileHelper)
    const files = fileHelper.readFile(pathName);
    const reader = new FileReader();
    console.log('pathName',pathName)
    reader.readAsText( files[0],'UTF-8');
    reader.onload = (readerEvent) => {
        if (readerEvent.target) {
            const content = readerEvent.target.result;
            callback(content);
        }
    };
}
