import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { createElement as _createElement } from "react";
// (C) 2022-2026 GoodData Corporation
import { useCallback, useMemo, useState } from "react";
import { useTheme } from "@gooddata/sdk-ui-theme-provider";
import { useDashboardSelector } from "../../model/react/DashboardStoreProvider.js";
import { selectConfig } from "../../model/store/config/configSelectors.js";
import { selectDashboardDescriptor, selectDashboardId } from "../../model/store/meta/metaSelectors.js";
import { selectCurrentUser } from "../../model/store/user/userSelectors.js";
import { useDashboardRelatedFilters } from "./hooks/useDashboardRelatedFilters.js";
import { useMetaExportData, useMetaExportImageData, useMetaPaletteData } from "./useExportData.js";
/**
 * @alpha
 */
export function DefaultDashboardExportVariables({ renderMode }) {
    const run = renderMode === "export";
    const exportData = useMetaExportData();
    const user = useDashboardSelector(selectCurrentUser);
    const conf = useDashboardSelector(selectConfig);
    const dashboardId = useDashboardSelector(selectDashboardId);
    const dashboardDescriptor = useDashboardSelector(selectDashboardDescriptor);
    const { dateFilters, attributeFilters, isError, isLoading } = useDashboardRelatedFilters(run);
    const theme = useTheme();
    if (!run) {
        return null;
    }
    return (_jsxs("div", { className: "gd-dashboard-meta", ...exportData?.root, children: [
            _jsx("div", { ...exportData?.id, children: dashboardId }), dashboardDescriptor.title ? _jsx("div", { ...exportData?.title, children: dashboardDescriptor.title }) : null, dashboardDescriptor.description ? (_jsx("div", { ...exportData?.description, children: dashboardDescriptor.description })) : null, dashboardDescriptor.tags && dashboardDescriptor.tags.length > 0 ? (_jsx("div", { ...exportData?.tags?.root, children: dashboardDescriptor.tags.map((tag, i) => (_jsx("span", { ...exportData?.tags?.tag, children: tag }, i))) })) : null, _jsx("div", { ...exportData?.filters?.root, ...exportData?.filters?.rootData(isLoading, isError), children: dateFilters.length > 0 || attributeFilters.length > 0 ? (_jsxs(_Fragment, { children: [dateFilters.map((filter, i) => (_jsxs("div", { ...exportData?.filters?.dateFilter, ...exportData?.filters?.filter.filterData(filter, isLoading, isError), children: [
                                _jsx("span", { ...exportData?.filters?.filter.name, children: filter?.title }),
                                " -", " ", _jsx("span", { ...exportData?.filters?.filter.value, children: filter?.subtitle })
                            ] }, i))), attributeFilters.map((filter, i) => (_jsxs("div", { ...exportData?.filters?.attributeFilter, ...exportData?.filters?.filter.filterData(filter, isLoading, isError), children: [
                                _jsx("span", { ...exportData?.filters?.filter.name, children: filter?.title }),
                                " -", " ", _jsx("span", { ...exportData?.filters?.filter.value, children: filter?.subtitle })
                            ] }, i)))] })) : null }), user?.fullName ? _jsx("div", { ...exportData?.user, children: user.fullName }) : null, conf.workspaceDescriptor ? (_jsx("div", { ...exportData?.workspace, children: conf.workspaceDescriptor.title })) : null, _jsx(MetaImage, { image: theme?.images?.logo, type: "logo" }), _jsx(MetaImage, { image: theme?.images?.coverImage, type: "cover-image" }), _jsx(Palette, { data: theme?.palette?.complementary })
        ] }));
}
function MetaImage({ image, type, }) {
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(false);
    const onLoad = useCallback(() => {
        setLoading(false);
    }, []);
    const onError = useCallback(() => {
        setError(true);
        setLoading(false);
    }, []);
    const exportData = useMetaExportImageData(type, loading, error);
    const url = useMemo(() => image?.replace(/url\((.*)\)/, "$1"), [image]);
    if (!url) {
        return null;
    }
    return _jsx("img", { src: url, onLoad: onLoad, onError: onError, alt: "", ...exportData });
}
function Palette({ data }) {
    const { exportData, item } = useMetaPaletteData();
    if (!data) {
        return null;
    }
    return (_jsx("div", { ...exportData, children: Object.entries(data).map(([key, value], i) => (_createElement("div", { ...item(key, value), key: i }))) }));
}
//# sourceMappingURL=DefaultDashboardExportVariables.js.map