import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { getHours, getMinutes, getSeconds } from '@wojtekmaj/date-utils';
import Hand from './Hand';
import MinuteMark from './MinuteMark';
import HourMark from './HourMark';
import type { formatHour as defaultFormatHour } from './shared/hourFormatter';
import {
isHandLength,
isOppositeHandLength,
isHandWidth,
isMarkLength,
isMarkWidth,
} from './shared/propTypes';
import type {
HandLength,
HandWidth,
MarkLength,
MarkWidth,
OppositeHandLength,
} from './shared/types';
type ClockProps = {
className?: string;
formatHour?: typeof defaultFormatHour;
hourHandLength?: HandLength;
hourHandOppositeLength?: OppositeHandLength;
hourHandWidth?: HandWidth;
hourMarksLength?: MarkLength;
hourMarksWidth?: MarkWidth;
locale?: string;
minuteHandLength?: HandLength;
minuteHandOppositeLength?: OppositeHandLength;
minuteHandWidth?: HandWidth;
minuteMarksLength?: MarkLength;
minuteMarksWidth?: MarkWidth;
renderHourMarks?: boolean;
renderMinuteHand?: boolean;
renderMinuteMarks?: boolean;
renderNumbers?: boolean;
renderSecondHand?: boolean;
secondHandLength?: HandLength;
secondHandOppositeLength?: OppositeHandLength;
secondHandWidth?: HandWidth;
size?: React.CSSProperties['width'];
value?: string | Date;
};
export default function Clock({
className,
formatHour,
hourHandLength = 50,
hourHandOppositeLength,
hourHandWidth = 4,
hourMarksLength = 10,
hourMarksWidth = 3,
locale,
minuteHandLength = 70,
minuteHandOppositeLength,
minuteHandWidth = 2,
minuteMarksLength = 6,
minuteMarksWidth = 1,
renderHourMarks = true,
renderMinuteHand = true,
renderMinuteMarks = true,
renderNumbers,
renderSecondHand = true,
secondHandLength = 90,
secondHandOppositeLength,
secondHandWidth = 1,
size = 150,
value,
}: ClockProps) {
function renderMinuteMarksFn() {
if (!renderMinuteMarks) {
return null;
}
const minuteMarks = [];
for (let i = 1; i <= 60; i += 1) {
const isHourMark = renderHourMarks && !(i % 5);
if (!isHourMark) {
minuteMarks.push(