function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
import _styled from "@emotion/styled/base";
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import { serializeTokens, variance } from '@codecademy/variance';
import { ThemeProvider, useTheme } from '@emotion/react';
import mapValues from 'lodash/mapValues';
import pick from 'lodash/pick';
import { createContext, forwardRef, useContext, useEffect, useMemo, useState } from 'react';
import { background, border, color, css, flex, grid, layout, positioning, space } from './variance/props';
import { styledOptions } from './variance/utils';
import { jsx as _jsx } from "react/jsx-runtime";
export var providerProps = variance.compose(layout, color, grid, flex, positioning, space, border, background);
export var modeColorProps = function modeColorProps(_ref) {
  var theme = _ref.theme,
    mode = _ref.mode;
  if (!theme || !mode || mode === (theme === null || theme === void 0 ? void 0 : theme.mode)) return {};
  var colors = theme.colors;
  return serializeTokens(mapValues(theme === null || theme === void 0 ? void 0 : theme.modes[mode], function (color) {
    return colors[color];
  }), 'color', theme).variables;
};
var BackgroundCurrentContext = /*#__PURE__*/createContext({
  'background-current': undefined
});
export function useColorModes() {
  var bgCurrent = useContext(BackgroundCurrentContext);
  var _ref2 = useTheme() || {},
    mode = _ref2.mode,
    modes = _ref2.modes,
    getColorValue = _ref2._getColorValue;
  var modesCopy = _objectSpread({}, modes);
  if (bgCurrent['background-current'] && modesCopy[mode]['background-current'] !== bgCurrent['background-current']) {
    /* sets the color to the copy of our modes object, and casts the type as the default color values for background-current.
    we could potentially alter the Merge type utility function from createTheme, but since 'background-current' is the only exception to the type-merging rule  and this is the only place we override, this seems to be a more straightforward + lower-risk solution.
    */

    modesCopy[mode]['background-current'] = bgCurrent['background-current'];
  }
  return [mode, modesCopy === null || modesCopy === void 0 ? void 0 : modesCopy[mode], modes, getColorValue];
}
export function useCurrentMode(mode) {
  var _useColorModes = useColorModes(),
    _useColorModes2 = _slicedToArray(_useColorModes, 1),
    activeMode = _useColorModes2[0];
  return mode !== null && mode !== void 0 ? mode : activeMode;
}
export function usePrefersDarkMode() {
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    prefersDarkMode = _useState2[0],
    setPrefersDarkMode = _useState2[1];
  useEffect(function () {
    function onChange(event) {
      setPrefersDarkMode(event.matches);
    }
    if (window && 'matchMedia' in window) {
      var mq = window.matchMedia('(prefers-color-scheme: dark)');
      if (mq && 'addEventListener' in mq) {
        setPrefersDarkMode(mq.matches);
        mq.addEventListener('change', onChange);
        return function () {
          return mq.removeEventListener('change', onChange);
        };
      }
    }
  }, []);
  return prefersDarkMode;
}
export var VariableProvider = /*#__PURE__*/_styled('div', _extends({}, {
  target: "e1skjn880",
  label: "VariableProvider"
}, styledOptions(['variables', 'alwaysSetVariables'])))(function (_ref3) {
  var variables = _ref3.variables;
  return variables;
}, css({
  textColor: 'text'
}), providerProps, process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,  */");
export var ColorMode = /*#__PURE__*/forwardRef(function (_ref4, ref) {
  var preference = _ref4.mode,
    alwaysSetVariables = _ref4.alwaysSetVariables,
    bg = _ref4.bg,
    rest = _objectWithoutProperties(_ref4, ["mode", "alwaysSetVariables", "bg"]);
  // checks if the user has set 'system' as their color mode preference
  // then sets the color mode
  var prefersDarkMode = usePrefersDarkMode();
  var mode = preference === 'system' ? prefersDarkMode ? 'dark' : 'light' : preference;
  var theme = useTheme();
  var modes = theme.modes,
    active = theme.mode,
    colors = theme.colors;
  var contextBg = bg ? 'background-current' : undefined;
  // This makes sure the background-current context is always set to the correct color + not the semantic color name.
  var currentParentBg = useContext(BackgroundCurrentContext);
  var bgCurrent = bg === 'background-current' ? currentParentBg['background-current'] ? currentParentBg['background-current'] : modes[active]['background-current'] : bg;

  /** Serialize color variables for the current mode
   * 1. If all variables are required add all mode variables to the current context
   * 2. If the user has specified a background color - set that color to the current-bg
   * 3. If not
   */
  var _useMemo = useMemo(function () {
      return serializeTokens(mapValues(modes[mode], function (color, key) {
        if (key === 'background-current' && typeof bg !== 'undefined') {
          return colors[bg];
        }
        return colors[color];
      }), 'color', theme);
    }, [colors, mode, modes, theme, bg]),
    variables = _useMemo.variables;
  if (active === mode) {
    var vars = alwaysSetVariables ? variables : pick(variables, ['--color-background-current']);
    return /*#__PURE__*/_jsx(BackgroundCurrentContext.Provider, {
      value: {
        'background-current': bgCurrent
      },
      children: /*#__PURE__*/_jsx(VariableProvider, _objectSpread(_objectSpread({}, rest), {}, {
        variables: vars,
        bg: contextBg,
        ref: ref
      }))
    });
  }
  return /*#__PURE__*/_jsx(ThemeProvider, {
    theme: {
      mode: mode
    },
    children: /*#__PURE__*/_jsx(VariableProvider, _objectSpread(_objectSpread({}, rest), {}, {
      variables: variables,
      bg: contextBg,
      ref: ref
    }))
  });
});