// @ts-nocheck
import * as React from 'react';
import ColDateUnitColumn from './ColDateUnitColumn';

function ColDateBody(props) {
  var generateConfig = props.generateConfig,
      prefixCls = props.prefixCls,
      operationRef = props.operationRef,
      activeColumnIndex = props.activeColumnIndex,
      value = props.value,
      _props$monthStep = props.monthStep,
      monthStep = _props$monthStep === void 0 ? 1 : _props$monthStep,
      _props$dateStep = props.dateStep,
      dateStep = _props$dateStep === void 0 ? 1 : _props$dateStep,
      disabledMonths = props.disabledMonths,
      disabledDates = props.disabledDates,
      hideDisabledOptions = props.hideDisabledOptions,
      onSelect = props.onSelect,
      viewDate = props.viewDate,
      locale = props.locale;
  var monthsLocale = locale.shortMonths || (generateConfig.locale.getShortMonths ? generateConfig.locale.getShortMonths(locale.locale) : []);

  var getCellTextMonth = function getCellTextMonth(date) {
    return locale.monthFormat ? formatValue(date, {
      locale: locale,
      format: locale.monthFormat,
      generateConfig: generateConfig
    }) : monthsLocale[generateConfig.getMonth(date)];
  };

  var baseMonth = generateConfig.setMonth(viewDate, 0);
  var baseDate = generateConfig.setDate(viewDate, 1);
  var monthLastDate = generateConfig.getEndDate(viewDate).format('DD');

  var getCellTextDate = function getCellTextDate(date) {
    return generateConfig.getDate(date);
  };

  var generateUnitsColDate = function generateUnitsColDate(start, end, step, disabledUnits, type) {
    var units = [];

    for (var i = start; i <= end; i += step) {
      if (type === 'month') {
        var getEachMonth = generateConfig.addMonth(baseMonth, i);
        units.push({
          label: getCellTextMonth(getEachMonth),
          value: getEachMonth,
          indexValue: i,
          disabled: (disabledUnits || []).includes(i + 1)
        });
      } else {
        var currentDate = generateConfig.addDate(baseDate, i);
        units.push({
          label: getCellTextDate(currentDate),
          value: currentDate,
          indexValue: i + 1,
          disabled: (disabledUnits || []).includes(i + 1)
        });
      }
    }

    return units;
  };

  var columns = [];
  var contentPrefixCls = "".concat(prefixCls, "-content");
  var columnPrefixCls = "".concat(prefixCls, "-time-panel");

  var _month = value || generateConfig.setMonth(generateConfig.getNow(), 0); // szp


  var _date = value || generateConfig.setDate(generateConfig.getNow(), 1); // szp


  var setChooseDate = function setChooseDate(chooseMonth, chooseDate) {
    var newDate = value || generateConfig.getNow();
    var chooseMonthNum = generateConfig.getMonth(chooseMonth);
    var chooseDateNum = generateConfig.getDate(chooseDate);
    newDate = generateConfig.setMonth(newDate, chooseMonthNum);
    newDate = generateConfig.setDate(newDate, chooseDateNum);
    return newDate;
  };

  var months = generateUnitsColDate(0, 11, monthStep, disabledMonths && disabledMonths(), 'month');
  var dates = generateUnitsColDate(0, monthLastDate - 1, dateStep, disabledDates && disabledDates(_date), 'date'); // ====================== Operations ======================

  operationRef.current = {
    onUpDown: function onUpDown(diff) {
      var column = columns[activeColumnIndex];

      if (column) {
        var valueIndex = column.units.findIndex(function (unit) {
          return unit.value === column.value;
        });
        var unitLen = column.units.length;

        for (var i = 1; i < unitLen; i += 1) {
          var nextUnit = column.units[(valueIndex + diff * i + unitLen) % unitLen];

          if (nextUnit.disabled !== true) {
            column.onSelect(nextUnit.value);
            break;
          }
        }
      }
    }
  }; // ======================== Render ========================

  function addColumnNode(condition, node, columnValue, units, onColumnSelect) {
    if (condition !== false) {
      columns.push({
        node: /*#__PURE__*/React.cloneElement(node, {
          prefixCls: columnPrefixCls,
          value: columnValue,
          active: activeColumnIndex === columns.length,
          onSelect: onColumnSelect,
          units: units,
          hideDisabledOptions: hideDisabledOptions
        }),
        onSelect: onColumnSelect,
        value: columnValue,
        units: units
      });
    }
  } // Month  szp


  addColumnNode(true, /*#__PURE__*/React.createElement(ColDateUnitColumn, {
    key: "month",
    dateDype: "month",
    generateConfig: generateConfig
  }), _month, months, function (num) {
    onSelect(setChooseDate(num, _date), 'mouse');
  }); // Date  szp

  addColumnNode(true, /*#__PURE__*/React.createElement(ColDateUnitColumn, {
    key: "date",
    dateDype: "date",
    generateConfig: generateConfig
  }), _date, dates, function (num) {
    onSelect(setChooseDate(_month, num), 'mouse');
  });
  return /*#__PURE__*/React.createElement("div", {
    className: contentPrefixCls
  }, columns.map(function (_ref) {
    var node = _ref.node;
    return node;
  }));
}

export default ColDateBody;