import { c$ as _baseSlice, d0 as _baseIndexOf, d1 as _hasUnicode, b1 as getDefaultExportFromCjs, d2 as _baseToString, d3 as _baseTrim, d4 as toString_1, i as __read } from './MapEditView-67426f04.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$1(array, start, end) {
  var length = array.length;
  end = end === undefined ? length : end;
  return (!start && end >= length) ? array : baseSlice(array, start, end);
}

var _castSlice = castSlice$1;

var baseIndexOf$1 = _baseIndexOf;

/**
 * Used by `_.trim` and `_.trimEnd` to get the index of the last 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 last unmatched string symbol.
 */
function charsEndIndex$1(strSymbols, chrSymbols) {
  var index = strSymbols.length;

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

var _charsEndIndex = charsEndIndex$1;

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$1(strSymbols, chrSymbols) {
  var index = -1,
      length = strSymbols.length;

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

var _charsStartIndex = charsStartIndex$1;

/**
 * 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$1(string) {
  return hasUnicode(string)
    ? unicodeToArray(string)
    : asciiToArray(string);
}

var _stringToArray = stringToArray$1;

var baseToString = _baseToString,
    baseTrim = _baseTrim,
    castSlice = _castSlice,
    charsEndIndex = _charsEndIndex,
    charsStartIndex = _charsStartIndex,
    stringToArray = _stringToArray,
    toString = toString_1;

/**
 * Removes leading and trailing whitespace or specified characters from `string`.
 *
 * @static
 * @memberOf _
 * @since 3.0.0
 * @category String
 * @param {string} [string=''] The string to trim.
 * @param {string} [chars=whitespace] The characters to trim.
 * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
 * @returns {string} Returns the trimmed string.
 * @example
 *
 * _.trim('  abc  ');
 * // => 'abc'
 *
 * _.trim('-_-abc-_-', '_-');
 * // => 'abc'
 *
 * _.map(['  foo  ', '  bar  '], _.trim);
 * // => ['foo', 'bar']
 */
function trim(string, chars, guard) {
  string = toString(string);
  if (string && (guard || chars === undefined)) {
    return baseTrim(string);
  }
  if (!string || !(chars = baseToString(chars))) {
    return string;
  }
  var strSymbols = stringToArray(string),
      chrSymbols = stringToArray(chars),
      start = charsStartIndex(strSymbols, chrSymbols),
      end = charsEndIndex(strSymbols, chrSymbols) + 1;

  return castSlice(strSymbols, start, end).join('');
}

var trim_1 = trim;

var trim$1 = /*@__PURE__*/getDefaultExportFromCjs(trim_1);

function useContextMenu(_a) {
    var triggerElement = _a.triggerElement, menuRender = _a.menuRender, _b = _a.disabled, disabled = _b === void 0 ? false : _b, _c = _a.triggerByChild, triggerByChild = _c === void 0 ? true : _c, wrapperClassName = _a.wrapperClassName, getPopupContainer = _a.getPopupContainer, getMenuPosition = _a.getMenuPosition, blackList = _a.blackList;
    var _d = __read(useMeasure(), 2), refFn = _d[0], _e = _d[1], width = _e.width, height = _e.height;
    var menuWrapperRef = useRef(null);
    var _f = __read(useState(false), 2), showMenu = _f[0], setShowMenu = _f[1];
    var _g = __read(useState(0), 2), xAxis = _g[0], setXAxis = _g[1];
    var _h = __read(useState(0), 2), yAxis = _h[0], setYAxis = _h[1];
    var menuStyle = useMemo(function () { return ({
        position: 'fixed',
        left: triggerElement && showMenu ? xAxis : "-".concat(window.outerWidth + 1000, "px"),
        top: triggerElement && showMenu ? yAxis : "-".concat(window.outerHeight + 1000, "px"),
        zIndex: 9999,
    }); }, [showMenu, triggerElement, xAxis, yAxis]);
    var refCallback = useCallback(function (node) {
        refFn(node);
        menuWrapperRef.current = node;
    }, [refFn]);
    useClickAway(menuWrapperRef, function () {
        // if (!menuWrapperRef.current?.contains(e.target as Node)) {
        setShowMenu(false);
        // }
    });
    var handleRightClick = useCallback(function (e) {
        var target = e.target, currentTarget = e.currentTarget;
        if (!target ||
            !currentTarget ||
            !(target instanceof Element) ||
            !(currentTarget instanceof Element) ||
            (triggerByChild && !currentTarget.contains(target)) ||
            (!triggerByChild && currentTarget !== target))
            return;
        if (blackList) {
            var len = blackList.length;
            var node = target;
            while (node) {
                for (var 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) {
            var _a = __read(getMenuPosition(e), 2), x = _a[0], y = _a[1];
            setXAxis(x);
            setYAxis(y);
            return;
        }
        var _b = e, _c = _b.clientX, clientX = _c === void 0 ? 0 : _c, _d = _b.clientY, clientY = _d === void 0 ? 0 : _d;
        // 屏幕边界处理
        var _e = document.documentElement, clientWidth = _e.clientWidth, clientHeight = _e.clientHeight;
        var offsetX = clientX + width - clientWidth;
        var offsetY = clientY + height - clientHeight;
        setXAxis(offsetX > 0 ? clientX - offsetX : clientX);
        setYAxis(offsetY > 0 ? clientY - offsetY : clientY);
    }, [blackList, getMenuPosition, height, triggerByChild, width]);
    var handleCloseMenu = useCallback(function () {
        setShowMenu(false);
    }, []);
    useEffect(function () {
        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 function () {
            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 { compact$1 as c, trim$1 as t, useContextMenu as u };
