import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useCurrentAppInfo } from "../../common/appInfo/CurrentAppInfoProvider.mjs";
import { useCurrentUserSettingsMenu } from "../../user/CurrentUserSettingsMenuProvider.mjs";
const CORE_REPO_URL = 'https://github.com/tegojs/tego';
const APP_REPO_URL = 'https://github.com/tegojs/tego-standard';
function getVersionUrl(version, repoUrl) {
    if (!version) return null;
    const hashMatch = version.match(/^\d+\.\d+\.\d+-([0-9a-f]{7})(?:@.+)?$/i);
    if (hashMatch) {
        const hash = hashMatch[1];
        return "".concat(repoUrl, "/commit/").concat(hash);
    }
    {
        const tag = version.startsWith('v') ? version : "v".concat(version);
        return "".concat(repoUrl, "/releases/tag/").concat(tag);
    }
}
const CoreVersion = ()=>{
    var _info_data_version, _info_data;
    const info = useCurrentAppInfo();
    const { t } = useTranslation();
    const version = null == info ? void 0 : null == (_info_data = info.data) ? void 0 : null == (_info_data_version = _info_data.version) ? void 0 : _info_data_version.core;
    const url = getVersionUrl(version, CORE_REPO_URL);
    return /*#__PURE__*/ jsxs("span", {
        children: [
            t('Core Version'),
            " -",
            ' ',
            url ? /*#__PURE__*/ jsx("a", {
                href: url,
                target: "_blank",
                children: version
            }) : version
        ]
    });
};
const AppVersion = ()=>{
    var _info_data_version, _info_data;
    const info = useCurrentAppInfo();
    const { t } = useTranslation();
    const version = null == info ? void 0 : null == (_info_data = info.data) ? void 0 : null == (_info_data_version = _info_data.version) ? void 0 : _info_data_version.app;
    const url = getVersionUrl(version, APP_REPO_URL);
    return /*#__PURE__*/ jsxs("span", {
        children: [
            t('App Version'),
            " -",
            ' ',
            url ? /*#__PURE__*/ jsx("a", {
                href: url,
                target: "_blank",
                children: version
            }) : version
        ]
    });
};
const SystemVersionProvider = (param)=>{
    let { children } = param;
    const { addMenuItem } = useCurrentUserSettingsMenu();
    useEffect(()=>{
        addMenuItem({
            key: 'system-version-app',
            label: /*#__PURE__*/ jsx(AppVersion, {})
        }, {
            before: 'divider_1'
        });
        addMenuItem({
            key: 'system-version-core',
            label: /*#__PURE__*/ jsx(CoreVersion, {})
        }, {
            before: 'divider_1'
        });
    }, [
        addMenuItem
    ]);
    return /*#__PURE__*/ jsx(Fragment, {
        children: children
    });
};
export { SystemVersionProvider };
