import _extends from "@babel/runtime/helpers/esm/extends";

var _ClockPin, _Typography, _Typography2;

import * as React from 'react';
import IconButton from '@mui/material/IconButton';
import Typography from '@mui/material/Typography';
import { styled } from '@mui/material/styles';
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
import ClockPointer from './ClockPointer';
import { useUtils } from '../internal/pickers/hooks/useUtils';
import { WrapperVariantContext } from '../internal/pickers/wrappers/WrapperVariantContext';
import { getHours, getMinutes } from './shared';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var ClockRoot = styled('div')(function (_ref) {
  var theme = _ref.theme;
  return {
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    margin: theme.spacing(2)
  };
});
var ClockClock = styled('div')({
  backgroundColor: 'rgba(0,0,0,.07)',
  borderRadius: '50%',
  height: 220,
  width: 220,
  flexShrink: 0,
  position: 'relative',
  pointerEvents: 'none'
});
var ClockSquareMask = styled('div')({
  width: '100%',
  height: '100%',
  position: 'absolute',
  pointerEvents: 'auto',
  outline: 0,
  // Disable scroll capabilities.
  touchAction: 'none',
  userSelect: 'none',
  '@media (pointer: fine)': {
    cursor: 'pointer',
    borderRadius: '50%'
  },
  '&:active': {
    cursor: 'move'
  }
});
var ClockPin = styled('div')(function (_ref2) {
  var theme = _ref2.theme;
  return {
    width: 6,
    height: 6,
    borderRadius: '50%',
    backgroundColor: theme.palette.primary.main,
    position: 'absolute',
    top: '50%',
    left: '50%',
    transform: 'translate(-50%, -50%)'
  };
});
var ClockAmButton = styled(IconButton)(function (_ref3) {
  var theme = _ref3.theme,
      ownerState = _ref3.ownerState;
  return _extends({
    zIndex: 1,
    position: 'absolute',
    bottom: ownerState.ampmInClock ? 64 : 8,
    left: 8
  }, ownerState.meridiemMode === 'am' && {
    backgroundColor: theme.palette.primary.main,
    color: theme.palette.primary.contrastText,
    '&:hover': {
      backgroundColor: theme.palette.primary.light
    }
  });
});
var ClockPmButton = styled(IconButton)(function (_ref4) {
  var theme = _ref4.theme,
      ownerState = _ref4.ownerState;
  return _extends({
    zIndex: 1,
    position: 'absolute',
    bottom: ownerState.ampmInClock ? 64 : 8,
    right: 8
  }, ownerState.meridiemMode === 'pm' && {
    backgroundColor: theme.palette.primary.main,
    color: theme.palette.primary.contrastText,
    '&:hover': {
      backgroundColor: theme.palette.primary.light
    }
  });
});
/**
 * @ignore - internal component.
 */

function Clock(props) {
  var ampm = props.ampm,
      ampmInClock = props.ampmInClock,
      autoFocus = props.autoFocus,
      children = props.children,
      date = props.date,
      getClockLabelText = props.getClockLabelText,
      handleMeridiemChange = props.handleMeridiemChange,
      isTimeDisabled = props.isTimeDisabled,
      meridiemMode = props.meridiemMode,
      _props$minutesStep = props.minutesStep,
      minutesStep = _props$minutesStep === void 0 ? 1 : _props$minutesStep,
      onChange = props.onChange,
      selectedId = props.selectedId,
      type = props.type,
      value = props.value;
  var ownerState = props;
  var utils = useUtils();
  var wrapperVariant = React.useContext(WrapperVariantContext);
  var isMoving = React.useRef(false);
  var isSelectedTimeDisabled = isTimeDisabled(value, type);
  var isPointerInner = !ampm && type === 'hours' && (value < 1 || value > 12);

  var handleValueChange = function handleValueChange(newValue, isFinish) {
    if (isTimeDisabled(newValue, type)) {
      return;
    }

    onChange(newValue, isFinish);
  };

  var setTime = function setTime(event, isFinish) {
    var _ref5 = event,
        offsetX = _ref5.offsetX,
        offsetY = _ref5.offsetY;

    if (offsetX === undefined) {
      var rect = event.target.getBoundingClientRect();
      offsetX = event.changedTouches[0].clientX - rect.left;
      offsetY = event.changedTouches[0].clientY - rect.top;
    }

    var newSelectedValue = type === 'seconds' || type === 'minutes' ? getMinutes(offsetX, offsetY, minutesStep) : getHours(offsetX, offsetY, Boolean(ampm));
    handleValueChange(newSelectedValue, isFinish);
  };

  var handleTouchMove = function handleTouchMove(event) {
    isMoving.current = true;
    setTime(event, 'shallow');
  };

  var handleTouchEnd = function handleTouchEnd(event) {
    if (isMoving.current) {
      setTime(event, 'finish');
      isMoving.current = false;
    }
  };

  var handleMouseMove = function handleMouseMove(event) {
    // event.buttons & PRIMARY_MOUSE_BUTTON
    if (event.buttons > 0) {
      setTime(event.nativeEvent, 'shallow');
    }
  };

  var handleMouseUp = function handleMouseUp(event) {
    if (isMoving.current) {
      isMoving.current = false;
    }

    setTime(event.nativeEvent, 'finish');
  };

  var hasSelected = React.useMemo(function () {
    if (type === 'hours') {
      return true;
    }

    return value % 5 === 0;
  }, [type, value]);
  var keyboardControlStep = type === 'minutes' ? minutesStep : 1;
  var listboxRef = React.useRef(null); // Since this is rendered when a Popper is opened we can't use passive effects.
  // Focusing in passive effects in Popper causes scroll jump.

  useEnhancedEffect(function () {
    if (autoFocus) {
      // The ref not being resolved would be a bug in MUI.
      listboxRef.current.focus();
    }
  }, [autoFocus]);

  var handleKeyDown = function handleKeyDown(event) {
    // TODO: Why this early exit?
    if (isMoving.current) {
      return;
    }

    switch (event.key) {
      case 'Home':
        // annulate both hours and minutes
        handleValueChange(0, 'partial');
        event.preventDefault();
        break;

      case 'End':
        handleValueChange(type === 'minutes' ? 59 : 23, 'partial');
        event.preventDefault();
        break;

      case 'ArrowUp':
        handleValueChange(value + keyboardControlStep, 'partial');
        event.preventDefault();
        break;

      case 'ArrowDown':
        handleValueChange(value - keyboardControlStep, 'partial');
        event.preventDefault();
        break;

      default: // do nothing

    }
  };

  return /*#__PURE__*/_jsxs(ClockRoot, {
    children: [/*#__PURE__*/_jsxs(ClockClock, {
      children: [/*#__PURE__*/_jsx(ClockSquareMask, {
        onTouchMove: handleTouchMove,
        onTouchEnd: handleTouchEnd,
        onMouseUp: handleMouseUp,
        onMouseMove: handleMouseMove
      }), !isSelectedTimeDisabled && /*#__PURE__*/_jsxs(React.Fragment, {
        children: [_ClockPin || (_ClockPin = /*#__PURE__*/_jsx(ClockPin, {})), date && /*#__PURE__*/_jsx(ClockPointer, {
          type: type,
          value: value,
          isInner: isPointerInner,
          hasSelected: hasSelected
        })]
      }), /*#__PURE__*/_jsx("div", {
        "aria-activedescendant": selectedId,
        "aria-label": getClockLabelText(type, date, utils),
        ref: listboxRef,
        role: "listbox",
        onKeyDown: handleKeyDown,
        tabIndex: 0,
        children: children
      })]
    }), ampm && (wrapperVariant === 'desktop' || ampmInClock) && /*#__PURE__*/_jsxs(React.Fragment, {
      children: [/*#__PURE__*/_jsx(ClockAmButton, {
        onClick: function onClick() {
          return handleMeridiemChange('am');
        },
        disabled: meridiemMode === null,
        ownerState: ownerState,
        children: _Typography || (_Typography = /*#__PURE__*/_jsx(Typography, {
          variant: "caption",
          children: "AM"
        }))
      }), /*#__PURE__*/_jsx(ClockPmButton, {
        disabled: meridiemMode === null,
        onClick: function onClick() {
          return handleMeridiemChange('pm');
        },
        ownerState: ownerState,
        children: _Typography2 || (_Typography2 = /*#__PURE__*/_jsx(Typography, {
          variant: "caption",
          children: "PM"
        }))
      })]
    })]
  });
}

export default Clock;