import { OFFICIAL_LIB_KEY } from '@cloudbase/cals';
import React from 'react';
export class ErrorBoundary extends React.Component {
    constructor() {
        super(...arguments);
        this.state = { error: null };
    }
    // capture error
    static getDerivedStateFromError(error) {
        return { error };
    }
    componentDidCatch(error, info) {
        var _a, _b;
        console.error('组件边界错误', error);
        (_b = (_a = this.props).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error, info);
    }
    render() {
        const { error } = this.state;
        // @ts-expect-error ts(2339) (at least one of these will be defined though, and we check for their existence)
        const { fallbackRender, FallbackComponent, fallback } = this.props;
        if (error !== null) {
            const props = {
                error,
            };
            if (React.isValidElement(fallback)) {
                return fallback;
            }
            if (typeof fallbackRender === 'function') {
                return fallbackRender(props);
            }
            if (FallbackComponent) {
                return React.createElement(FallbackComponent, Object.assign({}, props));
            }
            return `Error Render: ${error === null || error === void 0 ? void 0 : error.message}`;
        }
        return this.props.children;
    }
}
export function createErrorFallback(cmpKey, id) {
    return function ComponentErrorFallback(props) {
        let isComponentError = true;
        let tipsContent = '组件错误';
        return (React.createElement("div", { className: `component-error-box`, style: {
                width: 169,
                height: 91,
                background: 'rgb(241, 242, 245)',
                display: 'flex',
                justifyContent: 'center',
                alignItems: 'center',
                margin: '6px',
            }, title: isComponentError
                ? `组件${cmpKey} ${id}发生了错误，请确保您使用正确的组件库或联系组件作者错误信息：${props.error}`
                : tipsContent },
            React.createElement("div", { className: "component-error-box-icon", style: {
                    height: 32,
                    width: 32,
                    backgroundSize: '100% 100%',
                    backgroundRepeat: 'no-repeat',
                    backgroundImage: 'url(https://imgcache.qq.com/qcloud/lowcode/static/ide/component-fail.svg)',
                } })));
    };
}
export function ComponentNotFound({ module = OFFICIAL_LIB_KEY, component }) {
    return (React.createElement("div", { style: { color: 'red' } },
        "\u7EC4\u4EF6",
        React.createElement("em", null, `${module}:${component}`),
        "\u672A\u627E\u5230"));
}
