import React from 'react';
import { Button } from 'antd';
import { OakException, OakClockDriftException, } from 'oak-domain/es/types/Exception';
import ErrorMessage from './ErrorMessage';
import { ECode } from '../../../types/ErrorPage';
import ErrorPage from './ErrorPage';
import { getErrorPageCopy, translateErrorPageText, } from '../../../utils/errorPage';
function translateWithFallback(locales, key, fallback, params) {
    return translateErrorPageText((localeKey, localeParams) => locales.t(localeKey, localeParams), key, fallback, params);
}
function Error(props) {
    const { error, features } = props;
    const { locales } = features;
    const t = (key, fallback, params) => translateWithFallback(locales, key, fallback, params);
    const renderRefreshErrorPage = (code, copy) => (<ErrorPage code={code} title={t(copy.titleKey, copy.titleFallback)} desc={t(copy.descKey, copy.descFallback)} content={<ErrorMessage error={error}/>}>
            <Button type="primary" onClick={async () => {
            window.location.reload();
        }}>
                {t('common::refresh', '刷新')}
            </Button>
        </ErrorPage>);
    console.log(error);
    if (error instanceof OakException) {
        const copy = getErrorPageCopy(error);
        if (copy) {
            return renderRefreshErrorPage(copy.code, copy);
        }
        if (error instanceof OakClockDriftException) {
            // 服务器加密报错
            return (<React.Suspense fallback={null}>
                    <ErrorPage code={ECode.error} title={translateWithFallback(locales, 'error::clockDrift.title', '系统时间异常')} desc={translateWithFallback(locales, 'error::clockDrift.content', '您的系统时间与服务器时间差异过大，请同步设备时间后刷新重试。')} content={<ErrorMessage error={error}/>}>
                    <Button type="primary" onClick={async () => {
                    window.location.reload();
                }}>
                        {translateWithFallback(locales, 'common::refresh', '刷新')}
                    </Button>
                    </ErrorPage>
                </React.Suspense>);
        }
        const { _module, message } = error;
        return (<ErrorPage code={ECode.error} title={t('error::error.title', '内部异常')} desc={_module ? t(message, message, {
                '#oakModule': _module
            }) : message} content={<ErrorMessage error={error}/>}>
                <Button type="primary" onClick={async () => {
                window.location.reload();
            }}>
                    {t('common::refresh', '刷新')}
                </Button>
            </ErrorPage>);
    }
    const copy = getErrorPageCopy(error);
    if (copy) {
        return (<React.Suspense fallback={null}>
                {renderRefreshErrorPage(copy.code, copy)}
            </React.Suspense>);
    }
    return (<ErrorPage code={ECode.pageDataCacheFailure} title={t('error::unknown.title', '未知异常')} desc={t('error::unknown.content', '抱歉，出现未知异常，可能是数据缓存失效，需要清除缓存，请点击【清除缓存】再尝试')} content={<ErrorMessage error={error}/>}>
            <Button type="primary" onClick={async () => {
            await features.localStorage.clear();
            window.location.reload();
        }}>
                {t('common::clearCache', '清空缓存')}
            </Button>
        </ErrorPage>);
}
export default Error;
