import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2020-2026 GoodData Corporation
import { useState } from "react";
import { useIntl } from "react-intl";
import { invariant } from "ts-invariant";
import { useClientWorkspaceIdentifiers } from "@gooddata/sdk-ui";
import { Button, Dropdown } from "@gooddata/sdk-ui-kit";
import { useDashboardSelector } from "../../../../../model/react/DashboardStoreProvider.js";
import { selectBackendCapabilities } from "../../../../../model/store/backendCapabilities/backendCapabilitiesSelectors.js";
import { selectAllCatalogAttributesMap, selectAllCatalogDisplayFormsMap, } from "../../../../../model/store/catalog/catalogSelectors.js";
import { selectSettings } from "../../../../../model/store/config/configSelectors.js";
import { AttributeUrlSection } from "../../../../drill/DrillConfigPanel/DrillToUrl/AttributeUrlSection.js";
import { CustomUrlEditor } from "../../../../drill/DrillConfigPanel/DrillToUrl/CustomUrlEditor.js";
import { CustomUrlSection } from "../../../../drill/DrillConfigPanel/DrillToUrl/CustomUrlSection.js";
import { isDrillToAttributeUrlConfig, isDrillToCustomUrlConfig, } from "../../../../drill/types.js";
import { useAttributesWithDisplayForms } from "./useAttributesWithDisplayForms.js";
import { useInvalidAttributeDisplayFormIdentifiers } from "./useInvalidAttributeDisplayFormIdentifier.js";
function useButtonValue(urlDrillTarget) {
    const intl = useIntl();
    const displayForms = useDashboardSelector(selectAllCatalogDisplayFormsMap);
    const attributes = useDashboardSelector(selectAllCatalogAttributesMap);
    if (isDrillToCustomUrlConfig(urlDrillTarget) && urlDrillTarget.customUrl) {
        return urlDrillTarget.customUrl;
    }
    if (isDrillToAttributeUrlConfig(urlDrillTarget) && urlDrillTarget.drillToAttributeDisplayForm) {
        const displayForm = displayForms.get(urlDrillTarget.drillToAttributeDisplayForm);
        invariant(displayForm, "inconsistent state in drill to URL button");
        const attribute = attributes.get(displayForm.attribute);
        invariant(attribute, "inconsistent state in drill to URL button");
        return `${attribute.attribute.title} (${displayForm.title})`;
    }
    return intl.formatMessage({ id: "configurationPanel.drillIntoUrl.defaultButtonValue" });
}
const dropdownAlignPoints = [{ align: "bl tl" }, { align: "tl bl" }];
export function DrillTargetUrlItem(props) {
    const { onSelect, urlDrillTarget, attributes, widgetRef } = props;
    const capabilities = useDashboardSelector(selectBackendCapabilities);
    const settings = useDashboardSelector(selectSettings);
    const { allDisplayForms: targetAttributesFormsAll, linkDisplayForms: targetAttributesFormsWithLinks } = useAttributesWithDisplayForms(attributes);
    const invalidAttributeDisplayFormIdentifiers = useInvalidAttributeDisplayFormIdentifiers(urlDrillTarget, attributes);
    const [showModal, setShowModal] = useState(false);
    const toggleModal = () => setShowModal(!showModal);
    const onCustomUrlHandler = (customUrl) => {
        setShowModal(false);
        onSelect({ customUrl });
    };
    const onAttributeUrlHandler = (insightAttributeDisplayForm, drillToAttributeDisplayForm) => {
        onSelect({
            insightAttributeDisplayForm,
            drillToAttributeDisplayForm,
        });
    };
    const { client, dataProduct } = useClientWorkspaceIdentifiers();
    const buttonValue = useButtonValue(urlDrillTarget);
    return (_jsx(_Fragment, { children: _jsx(Dropdown, { closeOnParentScroll: true, alignPoints: dropdownAlignPoints, renderButton: ({ toggleDropdown, isOpen }) => (_jsx(Button, { onClick: toggleDropdown, className: "gd-button gd-button-primary button-dropdown dropdown-button gd-button-small s-drill-to-url-button", iconRight: isOpen ? "gd-icon-navigateup" : "gd-icon-navigatedown", value: buttonValue })), renderBody: ({ closeDropdown }) => (_jsxs("div", { className: "gd-menu-wrapper gd-drill-to-url-body gd-drill-to-url-list s-gd-drill-to-url-body", children: [capabilities.supportsHyperlinkAttributeLabels ? (_jsx(AttributeUrlSection, { attributeDisplayForms: targetAttributesFormsWithLinks, onSelect: (insightAttributeDisplayForm, drillToAttributeDisplayForm) => {
                            onAttributeUrlHandler(insightAttributeDisplayForm, drillToAttributeDisplayForm);
                            closeDropdown();
                        }, selected: isDrillToAttributeUrlConfig(urlDrillTarget) &&
                            urlDrillTarget.drillToAttributeDisplayForm })) : null, _jsx(CustomUrlSection, { ...props, urlDrillTarget: urlDrillTarget, toggleModal: toggleModal, closeDropdown: closeDropdown }), showModal ? (_jsx(CustomUrlEditor, { widgetRef: widgetRef, urlDrillTarget: urlDrillTarget, attributeDisplayForms: targetAttributesFormsAll, invalidAttributeDisplayFormIdentifiers: invalidAttributeDisplayFormIdentifiers, documentationLink: String(settings["drillIntoUrlDocumentationLink"] || ""), enableClientIdParameter: !!client, enableDataProductIdParameter: !!dataProduct, enableWidgetIdParameter: !!capabilities.supportsWidgetEntity, onSelect: (customUrl) => {
                            onCustomUrlHandler(customUrl);
                            closeDropdown();
                        }, onClose: toggleModal })) : null] })) }) }));
}
