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

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

var CustomYearsView = /** @class */ (function (_super) {
    __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 (React__default.createElement("td", __assign({}, props),
                React__default.createElement("span", __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()).year();
            var maxYear = maxDate ? maxDate.toObject().years : year + 100;
            var minYear = minDate ? minDate.toObject().years : year - 100;
            var title = __('Date.titleYear');
            var columns = [
                {
                    options: getRange(minYear, maxYear, 1)
                }
            ];
            return (React__default.createElement(Picker, { 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();
        _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(React__default.createElement('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 React__default.createElement("div", { className: "rdtYears" }, this.renderYearPicker());
        }
        return (React__default.createElement("div", { className: "rdtYears" },
            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(10, 'years') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('prev-year').getTestId()), "\u00AB"),
                        React__default.createElement("th", { className: "rdtSwitch" }, __('year-to-year', { from: year, to: year + 9 })),
                        React__default.createElement("th", __assign({ className: "rdtNext", onClick: this.props.addTime(10, '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.renderYears(year)))));
    };
    return CustomYearsView;
}(React__default.Component));
var CustomYearsView$1 = localeable(CustomYearsView);

export { CustomYearsView, CustomYearsView$1 as default };
