code: def lexsort_indexer(
      keys: Sequence[ArrayLike | Index | Series],
      orders=None,
      na_position: str = "last",
      key: Callable | None = None,
      codes_given: bool = False,
  ) -> npt.NDArray[np.intp]:
      """
      Performs lexical sorting on a set of keys
  
      Parameters
      ----------
      keys : Sequence[ArrayLike | Index | Series]
          Sequence of arrays to be sorted by the indexer
          Sequence[Series] is only if key is not None.
      orders : bool or list of booleans, optional
          Determines the sorting order for each element in keys. If a list,
          it must be the same length as keys. This determines whether the
          corresponding element in keys should be sorted in ascending
          (True) or descending (False) order. if bool, applied to all
          elements as above. if None, defaults to True.
      na_position : {'first', 'last'}, default 'last'
          Determines placement of NA elements in the sorted list ("last" or "first")
      key : Callable, optional
          Callable key function applied to every element in keys before sorting
      codes_given: bool, False
          Avoid categorical materialization if codes are already provided.
  
      Returns
      -------
      np.ndarray[np.intp]
      """
      from pandas.core.arrays import Categorical
  
      if na_position not in ["last", "first"]:
          raise ValueError(f"invalid na_position: {na_position}")
  
      if isinstance(orders, bool):
          orders = [orders] * len(keys)
      elif orders is None:
          orders = [True] * len(keys)
  
      labels = []
  
      for k, order in zip(keys, orders):
          k = ensure_key_mapped(k, key)
          if codes_given:
              codes = cast(np.ndarray, k)
              n = codes.max() + 1 if len(codes) else 0
          else:
              cat = Categorical(k, ordered=True)
              codes = cat.codes
              n = len(cat.categories)
  
          mask = codes == -1
  
          if na_position == "last" and mask.any():
              codes = np.where(mask, n, codes)
  
          # not order means descending
          if not order:
              codes = np.where(mask, codes, n - codes - 1)
  
          labels.append(codes)
  
      return np.lexsort(labels[::-1])
  
imports: from __future__ import annotations
  from collections import defaultdict
  from typing import TYPE_CHECKING, Callable, DefaultDict, cast
  import numpy
  from pandas._libs import algos, hashtable, lib
  from pandas._libs.hashtable import unique_label_indices
  from pandas.core.dtypes.common import ensure_int64, ensure_platform_int
  from pandas.core.dtypes.generic import ABCMultiIndex, ABCRangeIndex
  from pandas.core.dtypes.missing import isna
  from pandas.core.construction import extract_array
label_desc: Use image node_id 7 for calling a global function with example usage: lexsort_indexer(keys, orders, na_position, key, codes_given) and returns: np