/**
 * 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 QuarterView = /** @class */ (function (_super) {
    tslib.__extends(QuarterView, _super);
    function QuarterView() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            columns: [],
            pickerValue: [_this.props.viewDate.year(), _this.props.viewDate.quarter()]
        };
        _this.renderQuarter = function (props, quartar, 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()),
                    "Q",
                    quartar)));
        };
        _this.updateSelectedQuarter = function (event) {
            _this.props.updateSelectedDate(event);
        };
        _this.onPickerConfirm = function (value) {
            _this.props.onConfirm && _this.props.onConfirm(value, ['year', 'quarter']);
        };
        _this.onPickerChange = function (value, index) {
            _this.setState({ pickerValue: value });
        };
        return _this;
    }
    QuarterView.prototype.renderYear = function () {
        var _a = this.props, __ = _a.translate, testIdBuilder = _a.testIdBuilder;
        var showYearHead = !/^mm$/i.test(this.props.inputFormat || '');
        if (!showYearHead) {
            return null;
        }
        var canClick = /yy/i.test(this.props.inputFormat || '');
        return (_J$X_("table", null,
            _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")))));
    };
    QuarterView.prototype.renderQuarters = function () {
        var date = this.props.selectedDate, quarter = this.props.viewDate.quarter(), year = this.props.viewDate.year(), rows = [], i = 1, quarters = [], renderer = this.props.renderQuarter || this.renderQuarter, isValid = this.props.isValidDate || this.alwaysValidDate, classes, props, isDisabled;
        while (i < 5) {
            classes = 'rdtQuarter';
            isDisabled = !isValid(moment__default["default"]("".concat(year, "-").concat(i), 'YYYY-Q'));
            if (isDisabled)
                classes += ' rdtDisabled';
            if (date && i === date.quarter() && year === date.year())
                classes += ' rdtActive';
            props = {
                'key': i,
                'data-value': i,
                'className': classes
            };
            if (!isDisabled) {
                props.onClick =
                    this.props.updateOn === 'quarters'
                        ? this.updateSelectedQuarter
                        : this.props.setDate('quarter');
            }
            quarters.push(renderer(props, i, year, date && date.clone()));
            if (quarters.length === 2) {
                rows.push(_J$X_('tr', { key: quarter + '_' + rows.length }, quarters));
                quarters = [];
            }
            i++;
        }
        return rows;
    };
    QuarterView.prototype.alwaysValidDate = function () {
        return true;
    };
    QuarterView.prototype.cancel = function () {
        var _a, _b;
        (_b = (_a = this.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
    };
    QuarterView.prototype.renderPicker = function () {
        var __ = this.props.translate;
        var title = __('Date.titleQuarter');
        var minYear = new Date().getFullYear() - 100;
        var maxYear = new Date().getFullYear() + 100;
        var columns = [
            {
                options: amisCore.getRange(minYear, maxYear, 1)
            },
            {
                options: amisCore.getRange(1, 4).map(function (item) {
                    return {
                        text: 'Q' + item,
                        value: item
                    };
                })
            }
        ];
        return (_J$X_(Picker["default"], { translate: this.props.translate, locale: this.props.locale, title: title, columns: columns, value: this.state.pickerValue, onChange: this.onPickerChange, onConfirm: this.onPickerConfirm, onClose: this.cancel }));
    };
    QuarterView.prototype.render = function () {
        var _a = this.props, cx = _a.classnames, hideHeader = _a.hideHeader, mobileUI = _a.mobileUI;
        return mobileUI ? (this.renderPicker()) : (_J$X_("div", { className: cx('ClalendarQuarter') },
            hideHeader ? null : this.renderYear(),
            _J$X_("table", null,
                _J$X_("tbody", null, this.renderQuarters()))));
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], QuarterView.prototype, "cancel", null);
    return QuarterView;
}(React__default["default"].Component));
var QuartersView = amisCore.localeable(QuarterView);

exports.QuarterView = QuarterView;
exports["default"] = QuartersView;
