/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var moment = require('moment');
var React = require('react');
var amisCore = require('amis-core');
var Picker = require('../Picker.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var moment__default = /*#__PURE__*/_interopDefaultLegacy(moment);
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var CustomMonthsView = /** @class */ (function (_super) {
    tslib.__extends(CustomMonthsView, _super);
    function CustomMonthsView(props) {
        var _this = _super.call(this, props) || this;
        _this.renderMonth = function (props, month, year, date) {
            var _a = _this.props, __ = _a.translate, testIdBuilder = _a.testIdBuilder;
            var localMoment = props.viewDate, rest = tslib.__rest(props, ["viewDate"]);
            var monthStr = localMoment.month(month).format(__('MMM'));
            var strLength = 3;
            // Because some months are up to 5 characters long, we want to
            // use a fixed string length for consistency
            var monthStrFixedLength = monthStr.substring(0, strLength);
            return (_J$X_("td", tslib.__assign({}, rest),
                _J$X_("span", tslib.__assign({}, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild(props.key).getTestId()), monthStrFixedLength)));
        };
        _this.onConfirm = function (value, types) {
            _this.props.onConfirm && _this.props.onConfirm(value, ['year', 'month']);
        };
        _this.onPickerChange = function (value, index) {
            var _a = _this.props, maxDate = _a.maxDate, minDate = _a.minDate;
            var year = moment__default["default"]().year();
            var columns = tslib.__spreadArray([], tslib.__read(_this.state.columns), false);
            var maxDateObject = maxDate
                ? maxDate.toObject()
                : {
                    years: year + 100,
                    months: 11
                };
            var minDateObject = minDate
                ? minDate.toObject()
                : {
                    years: year - 100,
                    months: 0
                };
            var range = [];
            // 选择年份是最大值的年或者最小值的月时，需要重新计算月分选择的cloumn
            if (index === 0) {
                if (value[0] === minDateObject.years &&
                    value[0] === maxDateObject.years) {
                    range = amisCore.getRange(minDateObject.months, maxDateObject.months, 1);
                }
                else if (value[0] === minDateObject.years) {
                    range = amisCore.getRange(minDateObject.months, 11, 1);
                }
                else if (value[0] === maxDateObject.years) {
                    range = amisCore.getRange(0, maxDateObject.months, 1);
                }
                else {
                    range = amisCore.getRange(0, 11, 1);
                }
                columns[1] = {
                    options: range.map(function (i) {
                        return {
                            text: _this.props.timeCell(i + 1, 'month'),
                            value: i
                        };
                    })
                };
                _this.setState({
                    columns: _this.getColumnsWithUnit(columns),
                    pickerValue: value
                });
            }
        };
        _this.renderPicker = function () {
            var __ = _this.props.translate;
            var title = __('Date.titleMonth');
            return (_J$X_(Picker["default"], { translate: _this.props.translate, locale: _this.props.locale, title: title, columns: _this.state.columns, value: _this.state.pickerValue, onChange: _this.onPickerChange, onConfirm: _this.onConfirm, onClose: _this.props.onClose }));
        };
        var selectedDate = props.selectedDate, viewDate = props.viewDate;
        var currentDate = selectedDate || viewDate || moment__default["default"]();
        var dateBoundary = _this.props.getDateBoundary(currentDate);
        var columns = _this.props.getColumns(['year', 'month'], dateBoundary);
        _this.state = {
            columns: _this.getColumnsWithUnit(columns),
            pickerValue: currentDate.toArray()
        };
        _this.updateSelectedMonth = _this.updateSelectedMonth.bind(_this);
        return _this;
    }
    CustomMonthsView.prototype.renderMonths = function () {
        this.props.testIdBuilder;
        var date = this.props.selectedDate; this.props.viewDate.month(); var year = this.props.viewDate.year(), rows = [], i = 0, months = [], renderer = this.props.renderMonth || this.renderMonth, isValid = this.props.isValidDate || this.alwaysValidDate, classes, props, currentMonth, isDisabled, noOfDaysInMonth, daysInMonth, validDay, 
        // Date is irrelevant because we're only interested in month
        irrelevantDate = 1;
        while (i < 12) {
            classes = 'rdtMonth';
            currentMonth = this.props.viewDate
                .clone()
                .set({ year: year, month: i, date: irrelevantDate });
            noOfDaysInMonth = parseInt(currentMonth.endOf('month').format('D'), 10);
            daysInMonth = Array.from({ length: noOfDaysInMonth }, function (e, i) {
                return i + 1;
            });
            validDay = daysInMonth.find(function (d) {
                var day = currentMonth.clone().set('date', d);
                return isValid(day);
            });
            isDisabled = validDay === undefined;
            if (isDisabled)
                classes += ' rdtDisabled';
            if (date && i === date.month() && year === date.year())
                classes += ' rdtActive';
            props = {
                'key': i,
                'data-value': i,
                'className': classes,
                'viewDate': this.props.viewDate
            };
            if (!isDisabled)
                props.onClick =
                    this.props.updateOn === 'months'
                        ? this.updateSelectedMonth
                        : this.props.setDate && this.props.setDate('month');
            months.push(renderer(props, i, year, date && date.clone()));
            if (months.length === 3) {
                rows.push(_J$X_('tr', { key: i }, months));
                months = [];
            }
            i++;
        }
        return rows;
    };
    CustomMonthsView.prototype.updateSelectedMonth = function (event) {
        this.props.updateSelectedDate(event);
    };
    CustomMonthsView.prototype.getColumnsWithUnit = function (columns) {
        return this.props.locale === 'zh-CN' && columns.length === 2
            ? columns.map(function (item, index) {
                var _a;
                (_a = item.options) === null || _a === void 0 ? void 0 : _a.map(function (option) {
                    option.text = option.text + (index === 0 ? '年' : '月');
                    return option;
                });
                return item;
            })
            : columns;
    };
    CustomMonthsView.prototype.alwaysValidDate = function () {
        return true;
    };
    CustomMonthsView.prototype.render = function () {
        var __ = this.props.translate;
        var testIdBuilder = this.props.testIdBuilder;
        var showYearHead = !/^mm$/i.test(this.props.inputFormat || '') && !this.props.hideHeader;
        var canClick = /yy/i.test(this.props.inputFormat || '');
        if (this.props.mobileUI) {
            return _J$X_("div", { className: "rdtYears" }, this.renderPicker());
        }
        return (_J$X_("div", { className: "rdtMonths" },
            showYearHead && (_J$X_("table", { className: "headerTable" },
                _J$X_("thead", null,
                    _J$X_("tr", null,
                        _J$X_("th", tslib.__assign({ className: "rdtPrev", onClick: this.props.subtractTime(1, 'years') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('prev-year').getTestId()), "\u00AB"),
                        canClick ? (_J$X_("th", tslib.__assign({ className: "rdtSwitch", onClick: this.props.showView('years') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('switch-year').getTestId()), this.props.viewDate.format(__('dateformat.year')))) : (_J$X_("th", { className: "rdtSwitch" }, this.props.viewDate.format(__('dateformat.year')))),
                        _J$X_("th", tslib.__assign({ className: "rdtNext", onClick: this.props.addTime(1, 'years') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('next-year').getTestId()), "\u00BB"))))),
            _J$X_("table", null,
                _J$X_("tbody", null, this.renderMonths()))));
    };
    return CustomMonthsView;
}(React__default["default"].Component));
var CustomMonthsView$1 = amisCore.localeable(CustomMonthsView);

exports.CustomMonthsView = CustomMonthsView;
exports["default"] = CustomMonthsView$1;
