var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { memo, useCallback, useContext, useState, useRef, useMemo } from 'react';
import { pathEq } from 'ramda';
import { AdvancedMarker } from '@vis.gl/react-google-maps';
import { ProfileCard } from '../../../ProfileCard';
import { Popper } from '../../../Popper';
import { debounce, svg2Url } from '@reltio/mdm-sdk';
import { EntityMarkerContext } from '../../../contexts/EntityMarkerContext';
import { MARKER } from './icons/marker.inline.svg';
import { HOVERED_MARKER } from './icons/marker-hover.inline.svg';
import { useStyles } from './styles';
var HIDE_DEBOUNCE_INTERVAL = 50;
var MARKER_HEIGHT = 32;
var MARKER_CARD_MARGIN = 8;
var hoveredMarkerIcon = svg2Url(HOVERED_MARKER);
var markerIcon = svg2Url(MARKER);
var EntityMarker = function (_a) {
    var _b;
    var marker = _a.marker, onClick = _a.onClick, clusterer = _a.clusterer, otherProps = __rest(_a, ["marker", "onClick", "clusterer"]);
    var styles = useStyles();
    var _c = useState(false), hovered = _c[0], setHovered = _c[1];
    var activeEntityUri = useContext(EntityMarkerContext).activeEntityUri;
    var isActive = pathEq(['entity', 'uri'], activeEntityUri, marker);
    var position = useMemo(function () { return ({ lat: marker.point.lat, lng: marker.point.lng }); }, [marker.point]);
    var setHoveredDebounced = useCallback(debounce(setHovered, HIDE_DEBOUNCE_INTERVAL), []);
    var onMouseOver = useCallback(function () { return setHoveredDebounced(true); }, [setHoveredDebounced]);
    var onMouseOut = useCallback(function () { return setHoveredDebounced(false); }, [setHoveredDebounced]);
    var handleClick = useCallback(function () { return onClick(marker); }, [onClick, marker]);
    var advancedMarkerRef = useRef(null);
    var ref = useCallback(function (advancedMarker) {
        clusterer === null || clusterer === void 0 ? void 0 : clusterer(advancedMarker, marker.id);
        advancedMarkerRef.current = advancedMarker;
    }, [clusterer, marker.id]);
    var anchorEl = (_b = advancedMarkerRef.current) === null || _b === void 0 ? void 0 : _b.element;
    return (React.createElement(React.Fragment, null,
        anchorEl && (React.createElement(Popper, { anchorEl: anchorEl, open: hovered, className: styles.popper, modal: false, placement: "top", modifiers: [
                {
                    name: 'offset',
                    options: {
                        offset: [0, MARKER_HEIGHT + MARKER_CARD_MARGIN]
                    }
                }
            ] },
            React.createElement(ProfileCard, { entity: marker.entity, onMouseOver: onMouseOver, onMouseOut: onMouseOut }))),
        React.createElement(AdvancedMarker, __assign({ ref: ref, position: position, onClick: handleClick, onMouseEnter: onMouseOver, onMouseLeave: onMouseOut }, otherProps),
            React.createElement("img", { "data-reltio-id": "marker-icon", src: hovered || isActive ? hoveredMarkerIcon : markerIcon }))));
};
export default memo(EntityMarker);
