import { jsx, jsxs } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useState } from "react";
import { Helmet } from "react-helmet";
import { useTranslation } from "react-i18next";
import { Plugin } from "../../application/Plugin.mjs";
import { useCompile } from "../../schema-component/index.mjs";
import { useSystemSettings } from "../system-settings/index.mjs";
const DocumentTitleContext = /*#__PURE__*/ createContext({
    title: null,
    setTitle () {}
});
DocumentTitleContext.displayName = 'DocumentTitleContext';
const DocumentTitleProvider = (props)=>{
    const compile = useCompile();
    const { addonBefore, addonAfter } = props;
    const { t } = useTranslation();
    const [title, setTitle] = useState('');
    const documentTitle = "".concat(addonBefore ? " - ".concat(t(addonBefore)) : '').concat(t(compile(title) || '')).concat(addonAfter ? " - ".concat(t(addonAfter)) : '');
    return /*#__PURE__*/ jsxs(DocumentTitleContext.Provider, {
        value: {
            title,
            setTitle
        },
        children: [
            /*#__PURE__*/ jsx(Helmet, {
                children: /*#__PURE__*/ jsx("title", {
                    children: documentTitle
                })
            }),
            props.children
        ]
    });
};
const RemoteDocumentTitleProvider = (props)=>{
    var _ctx_data_data, _ctx_data;
    const ctx = useSystemSettings();
    return /*#__PURE__*/ jsx(DocumentTitleProvider, {
        addonAfter: null == ctx ? void 0 : null == (_ctx_data = ctx.data) ? void 0 : null == (_ctx_data_data = _ctx_data.data) ? void 0 : _ctx_data_data.title,
        children: props.children
    });
};
const useDocumentTitle = ()=>useContext(DocumentTitleContext);
const useCurrentDocumentTitle = (title)=>{
    const { setTitle } = useDocumentTitle();
    useEffect(()=>{
        setTitle(title);
    }, []);
};
class RemoteDocumentTitlePlugin extends Plugin {
    async load() {
        this.app.use(RemoteDocumentTitleProvider, this.options);
    }
}
export { DocumentTitleContext, DocumentTitleProvider, RemoteDocumentTitlePlugin, RemoteDocumentTitleProvider, useCurrentDocumentTitle, useDocumentTitle };
