/**
 * amis v3.5.1
 * build time: 2023-11-02
 * Copyright 2018-2023 baidu
 */

import { __extends, __assign, __decorate } from 'tslib';
import React from 'react';
import { getPropValue, normalizeDate, Renderer } from 'amis-core';
import 'moment-timezone';

var DateField = /** @class */ (function (_super) {
    __extends(DateField, _super);
    function DateField() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        // 动态显示相对时间时，用来触发视图更新
        _this.state = {
            random: 0
        };
        return _this;
    }
    DateField.prototype.componentDidMount = function () {
        var _this = this;
        var _a = this.props, fromNow = _a.fromNow, updateFrequency = _a.updateFrequency;
        if (fromNow && updateFrequency) {
            this.refreshInterval = setInterval(function () {
                _this.setState({
                    random: Math.random()
                });
            }, updateFrequency);
        }
    };
    DateField.prototype.componentWillUnmount = function () {
        clearInterval(this.refreshInterval);
    };
    DateField.prototype.render = function () {
        var _a = this.props, valueFormat = _a.valueFormat, format = _a.format, displayFormat = _a.displayFormat, placeholder = _a.placeholder, fromNow = _a.fromNow, className = _a.className, style = _a.style, cx = _a.classnames, locale = _a.locale, __ = _a.translate, displayTimeZone = _a.displayTimeZone;
        var viewValue = (React.createElement("span", { className: "text-muted" }, placeholder));
        var value = getPropValue(this.props);
        // 主要是给 fromNow 用的
        var date = null;
        if (value && (date = normalizeDate(value, valueFormat))) {
            var normalizeDate_1 = date;
            if (displayTimeZone) {
                normalizeDate_1 = normalizeDate_1.clone().tz(displayTimeZone);
            }
            viewValue = normalizeDate_1.format(displayFormat || format);
            if (viewValue) {
                date = viewValue;
            }
            if (fromNow) {
                viewValue = normalizeDate_1.locale(locale).fromNow();
            }
        }
        viewValue = !viewValue ? (React.createElement("span", { className: "text-danger" }, __('Date.invalid'))) : (viewValue);
        return (React.createElement("span", { className: cx('DateField', className), style: style, title: fromNow && date ? date : undefined }, viewValue));
    };
    DateField.defaultProps = {
        placeholder: '-',
        format: 'YYYY-MM-DD',
        valueFormat: 'X',
        fromNow: false,
        updateFrequency: 60000
    };
    return DateField;
}(React.Component));
/** @class */ ((function (_super) {
    __extends(DateFieldRenderer, _super);
    function DateFieldRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    DateFieldRenderer.defaultProps = __assign(__assign({}, DateField.defaultProps), { format: 'YYYY-MM-DD' });
    DateFieldRenderer = __decorate([
        Renderer({
            type: 'date'
        })
    ], DateFieldRenderer);
    return DateFieldRenderer;
})(DateField));
/** @class */ ((function (_super) {
    __extends(DateTimeFieldRenderer, _super);
    function DateTimeFieldRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    DateTimeFieldRenderer.defaultProps = __assign(__assign({}, DateField.defaultProps), { format: 'YYYY-MM-DD HH:mm:ss' });
    DateTimeFieldRenderer = __decorate([
        Renderer({
            type: 'datetime'
        })
    ], DateTimeFieldRenderer);
    return DateTimeFieldRenderer;
})(DateField));
/** @class */ ((function (_super) {
    __extends(TimeFieldRenderer, _super);
    function TimeFieldRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    TimeFieldRenderer.defaultProps = __assign(__assign({}, DateField.defaultProps), { format: 'HH:mm' });
    TimeFieldRenderer = __decorate([
        Renderer({
            type: 'time'
        })
    ], TimeFieldRenderer);
    return TimeFieldRenderer;
})(DateField));
/** @class */ ((function (_super) {
    __extends(MonthFieldRenderer, _super);
    function MonthFieldRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    MonthFieldRenderer.defaultProps = __assign(__assign({}, DateField.defaultProps), { format: 'YYYY-MM' });
    MonthFieldRenderer = __decorate([
        Renderer({
            type: 'month'
        })
    ], MonthFieldRenderer);
    return MonthFieldRenderer;
})(DateField));

export { DateField };
window.amisVersionInfo={version:'3.5.1',buildTime:'2023-11-02'};
