"use strict";
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var react_google_maps_1 = require("@vis.gl/react-google-maps");
var ProfileCard_1 = require("../../../ProfileCard");
var Popper_1 = require("../../../Popper");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var EntityMarkerContext_1 = require("../../../contexts/EntityMarkerContext");
var marker_inline_svg_1 = require("./icons/marker.inline.svg");
var marker_hover_inline_svg_1 = require("./icons/marker-hover.inline.svg");
var styles_1 = require("./styles");
var HIDE_DEBOUNCE_INTERVAL = 50;
var MARKER_HEIGHT = 32;
var MARKER_CARD_MARGIN = 8;
var hoveredMarkerIcon = (0, mdm_sdk_1.svg2Url)(marker_hover_inline_svg_1.HOVERED_MARKER);
var markerIcon = (0, mdm_sdk_1.svg2Url)(marker_inline_svg_1.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 = (0, styles_1.useStyles)();
    var _c = (0, react_1.useState)(false), hovered = _c[0], setHovered = _c[1];
    var activeEntityUri = (0, react_1.useContext)(EntityMarkerContext_1.EntityMarkerContext).activeEntityUri;
    var isActive = (0, ramda_1.pathEq)(['entity', 'uri'], activeEntityUri, marker);
    var position = (0, react_1.useMemo)(function () { return ({ lat: marker.point.lat, lng: marker.point.lng }); }, [marker.point]);
    var setHoveredDebounced = (0, react_1.useCallback)((0, mdm_sdk_1.debounce)(setHovered, HIDE_DEBOUNCE_INTERVAL), []);
    var onMouseOver = (0, react_1.useCallback)(function () { return setHoveredDebounced(true); }, [setHoveredDebounced]);
    var onMouseOut = (0, react_1.useCallback)(function () { return setHoveredDebounced(false); }, [setHoveredDebounced]);
    var handleClick = (0, react_1.useCallback)(function () { return onClick(marker); }, [onClick, marker]);
    var advancedMarkerRef = (0, react_1.useRef)(null);
    var ref = (0, react_1.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_1.default.createElement(react_1.default.Fragment, null,
        anchorEl && (react_1.default.createElement(Popper_1.Popper, { anchorEl: anchorEl, open: hovered, className: styles.popper, modal: false, placement: "top", modifiers: [
                {
                    name: 'offset',
                    options: {
                        offset: [0, MARKER_HEIGHT + MARKER_CARD_MARGIN]
                    }
                }
            ] },
            react_1.default.createElement(ProfileCard_1.ProfileCard, { entity: marker.entity, onMouseOver: onMouseOver, onMouseOut: onMouseOut }))),
        react_1.default.createElement(react_google_maps_1.AdvancedMarker, __assign({ ref: ref, position: position, onClick: handleClick, onMouseEnter: onMouseOver, onMouseLeave: onMouseOut }, otherProps),
            react_1.default.createElement("img", { "data-reltio-id": "marker-icon", src: hovered || isActive ? hoveredMarkerIcon : markerIcon }))));
};
exports.default = (0, react_1.memo)(EntityMarker);
