/**
 * amis-ui v6.14.0-beta.1
 * 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 CustomYearsView = /** @class */ (function (_super) {
    tslib.__extends(CustomYearsView, _super);
    function CustomYearsView(props) {
        var _this = _super.call(this, props) || this;
        _this.renderYear = function (props, year, date) {
            var testIdBuilder = _this.props.testIdBuilder;
            return (_J$X_("td", tslib.__assign({}, props),
                _J$X_("span", tslib.__assign({}, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild(props.key).getTestId()), year)));
        };
        _this.onConfirm = function (value) {
            _this.props.onConfirm && _this.props.onConfirm(value, ['year']);
        };
        _this.onPickerChange = function (value) {
            _this.setState({ pickerValue: value[0] });
        };
        _this.renderYearPicker = function () {
            var _a = _this.props, __ = _a.translate, minDate = _a.minDate, maxDate = _a.maxDate, selectedDate = _a.selectedDate, viewDate = _a.viewDate;
            var year = (selectedDate || viewDate || moment__default["default"]()).year();
            var maxYear = maxDate ? maxDate.toObject().years : year + 100;
            var minYear = minDate ? minDate.toObject().years : year - 100;
            var title = __('Date.titleYear');
            var columns = [
                {
                    options: amisCore.getRange(minYear, maxYear, 1)
                }
            ];
            return (_J$X_(Picker["default"], { translate: _this.props.translate, locale: _this.props.locale, title: title, columns: columns, value: _this.state.pickerValue, onConfirm: _this.onConfirm, onChange: _this.onPickerChange, onClose: _this.props.onClose }));
        };
        var selectedDate = props.selectedDate, viewDate = props.viewDate;
        var currentDate = selectedDate || viewDate || moment__default["default"]();
        _this.state = {
            pickerValue: currentDate.toObject().years
        };
        _this.updateSelectedYear = _this.updateSelectedYear.bind(_this);
        return _this;
    }
    CustomYearsView.prototype.renderYears = function (year) {
        var years = [], i = -1, rows = [], renderer = this.props.renderYear || this.renderYear, selectedDate = this.props.selectedDate, isValid = this.props.isValidDate || this.alwaysValidDate, classes, props, currentYear, isDisabled, noOfDaysInYear, daysInYear, validDay, 
        // Month and date are irrelevant here because
        // we're only interested in the year
        irrelevantMonth = 0, irrelevantDate = 1;
        year--;
        while (i < 11) {
            classes = 'rdtYear';
            currentYear = this.props.viewDate
                .clone()
                .set({ year: year, month: irrelevantMonth, date: irrelevantDate });
            noOfDaysInYear = parseInt(currentYear.endOf('year').format('DDD'), 10);
            daysInYear = Array.from({ length: noOfDaysInYear }, function (e, i) {
                return i + 1;
            });
            validDay = daysInYear.find(function (d) {
                var day = currentYear.clone().dayOfYear(d);
                return isValid(day);
            });
            isDisabled = validDay === undefined;
            if (isDisabled)
                classes += ' rdtDisabled';
            if (selectedDate && selectedDate.year() === year)
                classes += ' rdtActive';
            // 第一个和最后一个置灰
            if (i === -1 || i === 10)
                classes += ' text-muted';
            props = {
                'key': year,
                'data-value': year,
                'className': classes
            };
            if (!isDisabled)
                props.onClick =
                    this.props.updateOn === 'years'
                        ? this.updateSelectedYear
                        : this.props.setDate && this.props.setDate('year');
            years.push(renderer(props, year, selectedDate && selectedDate.clone()));
            if (years.length === 3) {
                rows.push(_J$X_('tr', { key: i }, years));
                years = [];
            }
            year++;
            i++;
        }
        return rows;
    };
    CustomYearsView.prototype.updateSelectedYear = function (event) {
        this.props.updateSelectedDate(event);
    };
    CustomYearsView.prototype.alwaysValidDate = function () {
        return true;
    };
    CustomYearsView.prototype.render = function () {
        var year = this.props.viewDate.year();
        year = year - (year % 10);
        var _a = this.props, testIdBuilder = _a.testIdBuilder, __ = _a.translate;
        if (this.props.mobileUI) {
            return _J$X_("div", { className: "rdtYears" }, this.renderYearPicker());
        }
        return (_J$X_("div", { className: "rdtYears" },
            _J$X_("table", { className: "headerTable" },
                _J$X_("thead", null,
                    _J$X_("tr", null,
                        _J$X_("th", tslib.__assign({ className: "rdtPrev", onClick: this.props.subtractTime(10, 'years') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('prev-year').getTestId()), "\u00AB"),
                        _J$X_("th", { className: "rdtSwitch" }, __('year-to-year', { from: year, to: year + 9 })),
                        _J$X_("th", tslib.__assign({ className: "rdtNext", onClick: this.props.addTime(10, 'years') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('next-year').getTestId()), "\u00BB")))),
            _J$X_("table", null,
                _J$X_("tbody", null, this.renderYears(year)))));
    };
    return CustomYearsView;
}(React__default["default"].Component));
var CustomYearsView$1 = amisCore.localeable(CustomYearsView);

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