import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useContext, useMemo } from 'react';
import cls from 'clsx';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

const CalenderDay = props => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('calendar', prefixCls);
  const style = useMemo(() => {
    const {
      item,
      index,
      color,
      offset,
      rowHeight
    } = props;
    const iternalStyle = {
      height: rowHeight
    };

    if (item.type === 'placeholder') {
      iternalStyle.width = '100%';
      return iternalStyle;
    }

    if (index === 0) {
      iternalStyle.marginLeft = `${100 * offset / 7}%`;
    }

    if (color) {
      switch (item.type) {
        case 'end':
        case 'start':
        case 'start-end':
        case 'multiple-middle':
        case 'multiple-selected':
          iternalStyle.background = color;
          break;

        case 'middle':
          iternalStyle.color = color;
          break;

        default:
          break;
      }
    }

    return iternalStyle;
  }, [props.item, props.index, props.color, props.offset, props.rowHeight]);

  const onClick = () => {
    var _a;

    if (props.item.type !== 'disabled') {
      (_a = props.onClick) === null || _a === void 0 ? void 0 : _a.call(props, props.item);
    }
  };

  const renderTopInfo = () => {
    const {
      topInfo
    } = props.item;

    if (topInfo || props.topInfoRender) {
      return _jsx("div", Object.assign({
        className: cls(bem('top-info'))
      }, {
        children: props.topInfoRender ? props.topInfoRender(props.item) : topInfo
      }));
    }

    return null;
  };

  const renderBottomInfo = () => {
    const {
      bottomInfo
    } = props.item;

    if (bottomInfo || props.bottomInfoRender) {
      return _jsx("div", Object.assign({
        className: cls(bem('bottom-info'))
      }, {
        children: props.bottomInfoRender ? props.bottomInfoRender(props.item) : bottomInfo
      }));
    }

    return null;
  };

  const renderContent = () => {
    const {
      item,
      color,
      rowHeight
    } = props;
    const {
      type,
      text
    } = item;

    const Nodes = _jsxs(_Fragment, {
      children: [renderTopInfo(), text, renderBottomInfo()]
    });

    if (type === 'selected') {
      return _jsx("div", Object.assign({
        className: cls(bem('selected-day')),
        style: {
          width: rowHeight,
          height: rowHeight,
          background: color
        }
      }, {
        children: Nodes
      }));
    }

    return Nodes;
  };

  const {
    type,
    className
  } = props.item;

  if (type === 'placeholder') {
    return _jsx("div", {
      className: cls(bem('day')),
      style: style
    });
  }

  return _jsx("div", Object.assign({
    role: "gridcell",
    style: style,
    className: cls(bem('day', type), className),
    tabIndex: type === 'disabled' ? undefined : -1,
    onClick: onClick
  }, {
    children: renderContent()
  }));
};

CalenderDay.defaultProps = {
  offset: 0
};
export default CalenderDay;