"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ROOT_COMPONENT () {
        return ROOT_COMPONENT;
    },
    get TOUR_BORDER_RADIUS_TOKEN () {
        return TOUR_BORDER_RADIUS_TOKEN;
    },
    get TOUR_FINDER_MARK () {
        return TOUR_FINDER_MARK;
    },
    get findRootElement () {
        return findRootElement;
    },
    get getHTMLElement () {
        return getHTMLElement;
    },
    get getIncreasedRadius () {
        return getIncreasedRadius;
    }
});
var _utils = require("../../../utils");
var TOUR_FINDER_MARK = 'data-tour';
var ROOT_COMPONENT = 'data-root';
var TOUR_BORDER_RADIUS_TOKEN = '--plasma-tour__border-radius';
var getHTMLElement = function getHTMLElement(target) {
    if (!target || !_utils.canUseDOM) {
        return null;
    }
    if (typeof target === 'string') {
        return document.querySelector(target);
    }
    if ('current' in target) {
        return target.current;
    }
    return target;
};
var getIncreasedRadius = function getIncreasedRadius(ref, offset) {
    if (!ref.current) {
        return '';
    }
    var element = ref.current;
    var computedStyle = getComputedStyle(element);
    var borderRadiusComponent = computedStyle.getPropertyValue(TOUR_BORDER_RADIUS_TOKEN);
    var currentRadius = computedStyle.borderRadius !== undefined && computedStyle.borderRadius !== null && computedStyle.borderRadius !== '0px' ? computedStyle.borderRadius : borderRadiusComponent;
    var parseValue = function parseValue(value, context) {
        var num = parseFloat(value);
        if (Number.isNaN(num)) return 0;
        if (value.endsWith('px')) return num;
        if (value.endsWith('%')) return num / 100 * context.width;
        if (value.endsWith('em')) return num * parseFloat(computedStyle.fontSize);
        if (value.endsWith('rem')) {
            return num * parseFloat(getComputedStyle(document.documentElement).fontSize);
        }
        if (value.endsWith('vw')) return num / 100 * window.innerWidth;
        if (value.endsWith('vh')) return num / 100 * window.innerHeight;
        if (value.endsWith('vmin')) return num / 100 * Math.min(window.innerWidth, window.innerHeight);
        if (value.endsWith('vmax')) return num / 100 * Math.max(window.innerWidth, window.innerHeight);
        return num;
    };
    var context = {
        width: element.offsetWidth,
        height: element.offsetHeight
    };
    var parseRadius = function parseRadius(radius) {
        var parts = radius.split('/').map(function(part) {
            return part.trim();
        });
        var horizontal = parts[0].split(/\s+/).map(function(val) {
            return parseValue(val, context);
        });
        var normalize = function normalize(values) {
            if (values.length === 1) return [
                values[0],
                values[0],
                values[0],
                values[0]
            ];
            if (values.length === 2) return [
                values[0],
                values[1],
                values[0],
                values[1]
            ];
            if (values.length === 3) return [
                values[0],
                values[1],
                values[2],
                values[1]
            ];
            return values.slice(0, 4);
        };
        return {
            horizontal: normalize(horizontal),
            vertical: parts[1] ? normalize(parts[1].split(/\s+/).map(function(val) {
                return parseValue(val, context);
            })) : normalize(horizontal)
        };
    };
    try {
        var parsed = parseRadius(currentRadius);
        var newHorizontal = parsed.horizontal.map(function(val) {
            return val !== 0 ? Math.max(0, val + offset) : 0;
        });
        var newVertical = parsed.vertical.map(function(val) {
            return val !== 0 ? Math.max(0, val + offset) : 0;
        });
        var format = function format(values) {
            return values.map(function(v) {
                return "".concat(v, "px");
            }).join(' ');
        };
        var horizontalStr = format(newHorizontal);
        var verticalStr = format(newVertical);
        return horizontalStr === verticalStr ? horizontalStr : "".concat(horizontalStr, " / ").concat(verticalStr);
    } catch (error) {
        return '';
    }
};
var findRootElement = function findRootElement(ref) {
    if (!ref.current) {
        return '';
    }
    var currentElement = ref.current;
    if (!(currentElement === null || currentElement === void 0 ? void 0 : currentElement.hasAttribute(TOUR_FINDER_MARK))) {
        return {
            current: currentElement
        };
    }
    while(currentElement !== null && currentElement !== document.documentElement){
        if (currentElement.hasAttribute(ROOT_COMPONENT)) {
            return {
                current: currentElement
            };
        }
        var parent = currentElement.parentElement;
        if (parent === null) {
            break;
        }
        currentElement = parent;
    }
    if (currentElement === null || currentElement === void 0 ? void 0 : currentElement.hasAttribute(ROOT_COMPONENT)) {
        return {
            current: currentElement
        };
    }
    return ref;
};
