import { cT as _baseSlice, cU as _baseIndexOf, cV as _hasUnicode, A as getDefaultExportFromCjs } from './MapEditView-f6381866.js';
import React__default, { useRef, useState, useMemo, useCallback, useEffect } from 'react';
import { createPortal } from 'react-dom';
import { useMeasure, useClickAway } from 'react-use';

var baseSlice = _baseSlice;

/**
 * Casts `array` to a slice if it's needed.
 *
 * @private
 * @param {Array} array The array to inspect.
 * @param {number} start The start position.
 * @param {number} [end=array.length] The end position.
 * @returns {Array} Returns the cast slice.
 */
function castSlice(array, start, end) {
  var length = array.length;
  end = end === undefined ? length : end;
  return (!start && end >= length) ? array : baseSlice(array, start, end);
}

var _castSlice = castSlice;

var baseIndexOf = _baseIndexOf;

/**
 * Used by `_.trim` and `_.trimStart` to get the index of the first string symbol
 * that is not found in the character symbols.
 *
 * @private
 * @param {Array} strSymbols The string symbols to inspect.
 * @param {Array} chrSymbols The character symbols to find.
 * @returns {number} Returns the index of the first unmatched string symbol.
 */
function charsStartIndex(strSymbols, chrSymbols) {
  var index = -1,
      length = strSymbols.length;

  while (++index < length && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {}
  return index;
}

var _charsStartIndex = charsStartIndex;

/**
 * Converts an ASCII `string` to an array.
 *
 * @private
 * @param {string} string The string to convert.
 * @returns {Array} Returns the converted array.
 */

function asciiToArray$1(string) {
  return string.split('');
}

var _asciiToArray = asciiToArray$1;

/** Used to compose unicode character classes. */

var rsAstralRange = '\\ud800-\\udfff',
    rsComboMarksRange = '\\u0300-\\u036f',
    reComboHalfMarksRange = '\\ufe20-\\ufe2f',
    rsComboSymbolsRange = '\\u20d0-\\u20ff',
    rsComboRange = rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange,
    rsVarRange = '\\ufe0e\\ufe0f';

/** Used to compose unicode capture groups. */
var rsAstral = '[' + rsAstralRange + ']',
    rsCombo = '[' + rsComboRange + ']',
    rsFitz = '\\ud83c[\\udffb-\\udfff]',
    rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')',
    rsNonAstral = '[^' + rsAstralRange + ']',
    rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}',
    rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]',
    rsZWJ = '\\u200d';

/** Used to compose unicode regexes. */
var reOptMod = rsModifier + '?',
    rsOptVar = '[' + rsVarRange + ']?',
    rsOptJoin = '(?:' + rsZWJ + '(?:' + [rsNonAstral, rsRegional, rsSurrPair].join('|') + ')' + rsOptVar + reOptMod + ')*',
    rsSeq = rsOptVar + reOptMod + rsOptJoin,
    rsSymbol = '(?:' + [rsNonAstral + rsCombo + '?', rsCombo, rsRegional, rsSurrPair, rsAstral].join('|') + ')';

/** Used to match [string symbols](https://mathiasbynens.be/notes/javascript-unicode). */
var reUnicode = RegExp(rsFitz + '(?=' + rsFitz + ')|' + rsSymbol + rsSeq, 'g');

/**
 * Converts a Unicode `string` to an array.
 *
 * @private
 * @param {string} string The string to convert.
 * @returns {Array} Returns the converted array.
 */
function unicodeToArray$1(string) {
  return string.match(reUnicode) || [];
}

var _unicodeToArray = unicodeToArray$1;

var asciiToArray = _asciiToArray,
    hasUnicode = _hasUnicode,
    unicodeToArray = _unicodeToArray;

/**
 * Converts `string` to an array.
 *
 * @private
 * @param {string} string The string to convert.
 * @returns {Array} Returns the converted array.
 */
function stringToArray(string) {
  return hasUnicode(string)
    ? unicodeToArray(string)
    : asciiToArray(string);
}

var _stringToArray = stringToArray;

function useContextMenu({ triggerElement, menuRender, disabled = false, triggerByChild = true, wrapperClassName, getPopupContainer, getMenuPosition, blackList, }) {
    const [refFn, { width, height }] = useMeasure();
    const menuWrapperRef = useRef(null);
    const [showMenu, setShowMenu] = useState(false);
    const [xAxis, setXAxis] = useState(0);
    const [yAxis, setYAxis] = useState(0);
    const menuStyle = useMemo(() => ({
        position: 'fixed',
        left: triggerElement && showMenu ? xAxis : `-${window.outerWidth + 1000}px`,
        top: triggerElement && showMenu ? yAxis : `-${window.outerHeight + 1000}px`,
        zIndex: 9999,
    }), [showMenu, triggerElement, xAxis, yAxis]);
    const refCallback = useCallback((node) => {
        refFn(node);
        menuWrapperRef.current = node;
    }, [refFn]);
    useClickAway(menuWrapperRef, () => {
        // if (!menuWrapperRef.current?.contains(e.target as Node)) {
        setShowMenu(false);
        // }
    });
    const handleRightClick = useCallback((e) => {
        const { target, currentTarget } = e;
        if (!target ||
            !currentTarget ||
            !(target instanceof Element) ||
            !(currentTarget instanceof Element) ||
            (triggerByChild && !currentTarget.contains(target)) ||
            (!triggerByChild && currentTarget !== target))
            return;
        if (blackList) {
            const len = blackList.length;
            let node = target;
            while (node) {
                for (let i = 0; i < len; i++) {
                    if (node.classList.contains(blackList[i]) || node.tagName === blackList[i])
                        return;
                }
                node = node.parentElement;
            }
        }
        e.preventDefault();
        setShowMenu(true);
        if (getMenuPosition) {
            const [x, y] = getMenuPosition(e);
            setXAxis(x);
            setYAxis(y);
            return;
        }
        const { clientX = 0, clientY = 0 } = e;
        // 屏幕边界处理
        const { clientWidth, clientHeight } = document.documentElement;
        const offsetX = clientX + width - clientWidth;
        const offsetY = clientY + height - clientHeight;
        setXAxis(offsetX > 0 ? clientX - offsetX : clientX);
        setYAxis(offsetY > 0 ? clientY - offsetY : clientY);
    }, [blackList, getMenuPosition, height, triggerByChild, width]);
    const handleCloseMenu = useCallback(() => {
        setShowMenu(false);
    }, []);
    useEffect(() => {
        if (disabled) {
            triggerElement === null || triggerElement === void 0 ? void 0 : triggerElement.removeEventListener('contextmenu', handleRightClick);
        }
        else {
            triggerElement === null || triggerElement === void 0 ? void 0 : triggerElement.addEventListener('contextmenu', handleRightClick);
        }
        return () => {
            triggerElement === null || triggerElement === void 0 ? void 0 : triggerElement.removeEventListener('contextmenu', handleRightClick);
        };
    }, [handleCloseMenu, handleRightClick, triggerElement, disabled]);
    return !disabled
        ? createPortal(React__default.createElement("div", { className: wrapperClassName, ref: refCallback, style: menuStyle, onClick: handleCloseMenu }, menuRender()), (getPopupContainer === null || getPopupContainer === void 0 ? void 0 : getPopupContainer()) || document.body)
        : null;
}

/**
 * Creates an array with all falsey values removed. The values `false`, `null`,
 * `0`, `""`, `undefined`, and `NaN` are falsey.
 *
 * @static
 * @memberOf _
 * @since 0.1.0
 * @category Array
 * @param {Array} array The array to compact.
 * @returns {Array} Returns the new array of filtered values.
 * @example
 *
 * _.compact([0, 1, false, 2, '', 3]);
 * // => [1, 2, 3]
 */

function compact(array) {
  var index = -1,
      length = array == null ? 0 : array.length,
      resIndex = 0,
      result = [];

  while (++index < length) {
    var value = array[index];
    if (value) {
      result[resIndex++] = value;
    }
  }
  return result;
}

var compact_1 = compact;

var compact$1 = /*@__PURE__*/getDefaultExportFromCjs(compact_1);

export { _castSlice as _, _charsStartIndex as a, _stringToArray as b, compact$1 as c, useContextMenu as u };
