/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __extends, __rest, __assign, __spreadArray, __read } from 'tslib';
import moment from 'moment';
import React__default from 'react';
import { getRange, localeable } from 'amis-core';
import Picker from '../Picker.js';

var CustomMonthsView = /** @class */ (function (_super) {
    __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 = __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 (React__default.createElement("td", __assign({}, rest),
                React__default.createElement("span", __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().year();
            var columns = __spreadArray([], __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 = getRange(minDateObject.months, maxDateObject.months, 1);
                }
                else if (value[0] === minDateObject.years) {
                    range = getRange(minDateObject.months, 11, 1);
                }
                else if (value[0] === maxDateObject.years) {
                    range = getRange(0, maxDateObject.months, 1);
                }
                else {
                    range = 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 (React__default.createElement(Picker, { 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();
        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(React__default.createElement('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 React__default.createElement("div", { className: "rdtYears" }, this.renderPicker());
        }
        return (React__default.createElement("div", { className: "rdtMonths" },
            showYearHead && (React__default.createElement("table", { className: "headerTable" },
                React__default.createElement("thead", null,
                    React__default.createElement("tr", null,
                        React__default.createElement("th", __assign({ className: "rdtPrev", onClick: this.props.subtractTime(1, 'years') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('prev-year').getTestId()), "\u00AB"),
                        canClick ? (React__default.createElement("th", __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')))) : (React__default.createElement("th", { className: "rdtSwitch" }, this.props.viewDate.format(__('dateformat.year')))),
                        React__default.createElement("th", __assign({ className: "rdtNext", onClick: this.props.addTime(1, 'years') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('next-year').getTestId()), "\u00BB"))))),
            React__default.createElement("table", null,
                React__default.createElement("tbody", null, this.renderMonths()))));
    };
    return CustomMonthsView;
}(React__default.Component));
var CustomMonthsView$1 = localeable(CustomMonthsView);

export { CustomMonthsView, CustomMonthsView$1 as default };
