import { __rest } from "tslib";
import { REPORT_NAME } from '@codeblitzjs/ide-sumi-core';
import { getDebugLogger, IReporterService, localize } from '@opensumi/ide-core-common';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { createRoot } from 'react-dom/client';
import { useConstant } from '../core/hooks';
import { IPropsService, PropsServiceImpl } from '../core/props.service';
import { Root } from '../core/Root';
import styles from '../core/style.module.less';
import { createEditor } from './createEditor';
export const renderEditor = (domElement, props) => {
    const { onLoad, Landing } = props, opts = __rest(props, ["onLoad", "Landing"]);
    const app = createEditor(opts);
    const className = `codeblitz-editor ${opts.runtimeConfig.hideEditorTab ? styles['hide-editor-tab'] : ''}`;
    const root = createRoot(domElement);
    root.render(React.createElement(Root, { status: 'loading', Landing: Landing, className: className }));
    app
        .start((IDEApp) => {
        return new Promise((resolve) => {
            root.render(React.createElement(Root, { status: 'success', className: className },
                React.createElement(IDEApp, null)));
        });
    })
        .then(() => {
        onLoad === null || onLoad === void 0 ? void 0 : onLoad(app);
    })
        .catch((err) => {
        root.render(React.createElement(Root, { status: 'error', error: (err === null || err === void 0 ? void 0 : err.message) || localize('error.unknown'), className: className }));
        app.injector.get(IReporterService).point(REPORT_NAME.APP_START_ERROR, err === null || err === void 0 ? void 0 : err.message, { error: err });
        getDebugLogger().error(err);
        setTimeout(() => {
            throw err;
        });
    });
    return () => {
        app.destroy();
    };
};
export const EditorRenderer = (_a) => {
    var { onLoad, Landing } = _a, opts = __rest(_a, ["onLoad", "Landing"]);
    const app = useConstant(() => createEditor(opts));
    const ideAppRef = useRef(null);
    const propsService = useConstant(() => new PropsServiceImpl());
    propsService.props = { documentModel: opts.documentModel, editorConfig: opts.editorConfig };
    const [state, setState] = useState(() => ({ status: 'loading' }));
    useEffect(() => {
        app.injector.addProviders({
            token: IPropsService,
            useValue: propsService,
        });
        app
            .start((IDEApp) => {
            ideAppRef.current = IDEApp;
            setState({ status: 'success' });
            return Promise.resolve();
        })
            .then(() => {
            onLoad === null || onLoad === void 0 ? void 0 : onLoad(app);
        })
            .catch((err) => {
            setState({ error: (err === null || err === void 0 ? void 0 : err.message) || localize('error.unknown'), status: 'error' });
            app.injector.get(IReporterService).point(REPORT_NAME.APP_START_ERROR, err === null || err === void 0 ? void 0 : err.message, {
                error: err,
            });
            getDebugLogger().error(err);
            setTimeout(() => {
                throw err;
            });
        });
        return () => {
            app.destroy();
        };
    }, []);
    const rootClassName = useMemo(() => (opts.runtimeConfig.hideEditorTab ? styles['hide-editor-tab'] : ''), [opts.runtimeConfig.hideEditorTab]);
    return (React.createElement(Root, Object.assign({}, state, { Landing: Landing, className: `codeblitz-editor ${rootClassName}` }), ideAppRef.current ? React.createElement(ideAppRef.current, null) : null));
};
//# sourceMappingURL=renderEditor.js.map