var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import React from 'react';
import { MaskCharLowLine } from '../../internal/MaskCharLowLine/index.js';
import { InternalDateValidator } from '../../lib/date/InternalDateValidator.js';
import { InternalDateComponentType } from '../../lib/date/types.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { getStyles } from './DateFragmentsView.styles.js';
var DateFragmentsView = /** @class */ (function (_super) {
    __extends(DateFragmentsView, _super);
    function DateFragmentsView() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.rootNode = null;
        _this.isFragment = function (el) {
            if (_this.rootNode) {
                // NOTE: NodeList doesn't support method 'forEach' in IE11
                var fragments = Array.from(_this.rootNode.querySelectorAll('[data-fragment]'));
                return fragments.some(function (fragment) { return fragment === el || fragment.contains(el); });
            }
            return false;
        };
        _this.getRootNode = function () { return _this.rootNode; };
        _this.rootRef = function (el) {
            _this.rootNode = el;
        };
        return _this;
    }
    DateFragmentsView.prototype.render = function () {
        var _this = this;
        this.styles = getStyles(this.emotion);
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = theme;
            return _this.renderMain();
        }));
    };
    DateFragmentsView.prototype.renderMain = function () {
        var _a;
        var _this = this;
        return (React.createElement("span", { ref: this.rootRef, className: this.cx((_a = {},
                _a[this.styles.root()] = true,
                _a[this.styles.selected(this.theme)] = true,
                _a)) }, this.props.fragments.map(function (fragment, index) {
            return fragment.type === InternalDateComponentType.Separator
                ? _this.renderSeparator(fragment, index)
                : _this.renderDateComponent(fragment, index);
        })));
    };
    DateFragmentsView.prototype.renderSeparator = function (fragment, index) {
        var _a;
        var separatorClassName = this.cx((_a = {},
            _a[this.styles.mask(this.theme)] = true,
            _a[this.styles.delimiterFilled()] = this.props.fragments[index + 1].value !== null,
            _a));
        return (React.createElement("span", { key: index, className: separatorClassName }, fragment.value));
    };
    DateFragmentsView.prototype.renderDateComponent = function (fragment, index) {
        var _this = this;
        var _a = this.props, inputMode = _a.inputMode, onSelectDateComponent = _a.onSelectDateComponent, selected = _a.selected;
        var type = fragment.type, value = fragment.value, length = fragment.length, valueWithPad = fragment.valueWithPad;
        var getValueMask = function () {
            if (value === null || (selected === type && inputMode)) {
                return value;
            }
            return valueWithPad || value;
        };
        var valueMask = getValueMask();
        var lengthMask = InternalDateValidator.testParseToNumber(valueMask)
            ? Math.max(length - valueMask.toString().length, 0)
            : length;
        var handleMouseUp = function (e) {
            var _a, _b;
            if ((_b = (_a = _this.globalObject.document) === null || _a === void 0 ? void 0 : _a.activeElement) === null || _b === void 0 ? void 0 : _b.contains(e.currentTarget)) {
                onSelectDateComponent(type, e);
            }
        };
        return (React.createElement("span", { key: index, "data-fragment": "", onMouseUp: handleMouseUp },
            valueMask,
            React.createElement("span", { "data-tid": "DateFragmentsView__placeholder", className: this.styles.mask(this.theme) }, Array(lengthMask)
                .fill('')
                .map(function (_, i) { return (React.createElement(MaskCharLowLine, { key: i })); }))));
    };
    DateFragmentsView = __decorate([
        withRenderEnvironment
    ], DateFragmentsView);
    return DateFragmentsView;
}(React.Component));
export { DateFragmentsView };
//# sourceMappingURL=DateFragmentsView.js.map