var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import React from 'react';
import warning from 'warning';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { PopupHelper } from './PopupHelper.js';
import { getStyles } from './PopupPin.styles.js';
import { PopupDataTids } from './Popup.js';
var PopupPin = /** @class */ (function (_super) {
    __extends(PopupPin, _super);
    function PopupPin() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getPinInlineStyle = function (left, top) {
            var _a, _b;
            var direction = _this.getPopupOppositeDirection();
            if (!direction) {
                return;
            }
            // Добавляем запас в 1px, чтобы пофиксить случайные отделения пинов
            var correctedLeft = left - 1;
            var correctedTop = top - 1;
            var correctedSize = _this.props.size + 1;
            var defaultPinInlineStyle = (_a = {},
                _a[direction] = -_this.props.size + 'px',
                _a.left = correctedLeft + 'px',
                _a.width = correctedSize * 2 + 'px',
                _a.height = correctedSize + 'px',
                _a.backgroundColor = _this.props.backgroundColor,
                _a);
            switch (direction) {
                case 'top':
                case 'bottom':
                    return defaultPinInlineStyle;
                case 'left':
                case 'right':
                    return _b = {},
                        _b[direction] = -_this.props.size + 'px',
                        _b.top = correctedTop + 'px',
                        _b.height = correctedSize * 2 + 'px',
                        _b.width = correctedSize + 'px',
                        _b.backgroundColor = _this.props.backgroundColor,
                        _b;
                default:
                    warning(false, "Can't get inline style: invalid direction '".concat(direction, "'. Must be one of - 'top', 'right', 'bottom', 'left'."));
                    return defaultPinInlineStyle;
            }
        };
        _this.getPinCoordinates = function (popupElement) {
            var popupRect = PopupHelper.getElementAbsoluteRect(popupElement);
            var _a = _this.positionObject, direction = _a.direction, align = _a.align;
            var defaultPinCoordinates = {
                top: popupRect.height,
                left: _this.getPinLeftCoordinate(popupRect, align),
            };
            switch (direction) {
                case 'top':
                    return defaultPinCoordinates;
                case 'bottom':
                    return {
                        top: -2 * _this.props.size,
                        left: _this.getPinLeftCoordinate(popupRect, align),
                    };
                case 'left':
                    return {
                        top: _this.getPinTopCoordinate(popupRect, align),
                        left: popupRect.width,
                    };
                case 'right':
                    return {
                        top: _this.getPinTopCoordinate(popupRect, align),
                        left: -2 * _this.props.size,
                    };
                default:
                    warning(false, "Can't get coordinates: invalid direction '".concat(direction, "'. Must be one of - 'top', 'right', 'bottom', 'left'."));
                    return defaultPinCoordinates;
            }
        };
        _this.getPinTopCoordinate = function (popupRect, align) {
            var defaultTopCoordinate = _this.props.offset;
            switch (align) {
                case 'top':
                    return defaultTopCoordinate;
                case 'middle':
                    return popupRect.height / 2 - _this.props.size;
                case 'bottom':
                    return popupRect.height - _this.props.offset - 2 * _this.props.size;
                default:
                    warning(false, "Can't get top coordinate: invalid align '".concat(align, "'. Must be one of - 'top', 'middle', 'bottom'."));
                    return defaultTopCoordinate;
            }
        };
        _this.getPinLeftCoordinate = function (popupRect, align) {
            var defaultLetfCoordinate = popupRect.width / 2 - _this.props.size;
            switch (align) {
                case 'left':
                    return _this.props.offset;
                case 'center':
                    return defaultLetfCoordinate;
                case 'right':
                    return popupRect.width - _this.props.offset - 2 * _this.props.size;
                default:
                    warning(false, "Can't get left coordinate: invalid align '".concat(align, "'. Must be one of - 'left', 'center', 'right'."));
                    return defaultLetfCoordinate;
            }
        };
        _this.getPinDirectionalStyle = function () {
            switch (_this.positionObject.direction) {
                case 'top':
                    return _this.styles.pinTop();
                case 'bottom':
                    return _this.styles.pinBottom();
                case 'left':
                    return _this.styles.pinLeft();
                case 'right':
                    return _this.styles.pinRight();
                default:
                    warning(false, "Can't get directional style: invalid direction '".concat(_this.positionObject.direction, "'. Must be one of - 'top', 'right', 'bottom', 'left'."));
                    return _this.styles.pinTop();
            }
        };
        return _this;
    }
    PopupPin.prototype.render = function () {
        this.styles = getStyles(this.emotion);
        if (!this.props.popupElement) {
            return null;
        }
        this.positionObject = PopupHelper.getPositionObject(this.props.popupPosition);
        var coords = this.getPinCoordinates(this.props.popupElement);
        if (!coords || !coords.left || !coords.top) {
            return null;
        }
        var inlineStyle = this.getPinInlineStyle(coords.left, coords.top);
        var directionalStyle = this.getPinDirectionalStyle();
        if (!inlineStyle || !directionalStyle) {
            return null;
        }
        return (React.createElement("div", { "data-tid": PopupDataTids.popupPin, className: this.cx(this.styles.pin(), directionalStyle), style: inlineStyle }));
    };
    PopupPin.prototype.getPopupOppositeDirection = function () {
        var popupDirection = PopupHelper.getPositionObject(this.props.popupPosition).direction;
        switch (popupDirection) {
            case 'top':
                return 'bottom';
            case 'bottom':
                return 'top';
            case 'left':
                return 'right';
            case 'right':
                return 'left';
            default:
                warning(false, "Can't get opposite direction: invalid direction ".concat(popupDirection, ". Must be one of - 'top', 'bottom', 'left', 'right'."));
                return 'bottom';
        }
    };
    PopupPin.__KONTUR_REACT_UI__ = 'PopupPin';
    PopupPin.displayName = 'PopupPin';
    PopupPin = __decorate([
        withRenderEnvironment
    ], PopupPin);
    return PopupPin;
}(React.Component));
export { PopupPin };
//# sourceMappingURL=PopupPin.js.map