"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormikDatepicker = void 0;
var React = require("react");
var pickers_1 = require("@material-ui/pickers");
var FormikField_1 = require("./FormikField");
var utils_1 = require("./utils");
var makeStyles_1 = require("@material-ui/core/styles/makeStyles");
var defaultProps = {
    margin: "normal",
    style: { minWidth: "240px" },
    placeholder: "tt.mm.jjjj",
    format: "dd.MM.yyyy",
    autoOk: true,
    variant: "inline",
};
var utils = {
    moment: "MomentUtils",
    dateFns: "DateFnsUtils",
};
var useStyles = (0, makeStyles_1.default)({
    datePickerStyles: function (props) { return ({
        "& p": {
            color: "red" // it was grey when not empty so had to override for consistency
        },
        "& button": {
            display: props.removeButton ? "none" : undefined
        }
    }); },
});
function FormikDatepicker(props) {
    var name = props.name, error = props.error, useField = props.useField, helperText = props.helperText, formHelperTextProps = props.formHelperTextProps, fieldProps = props.fieldProps, validate = props.validate, others = __rest(props, ["name", "error", "useField", "helperText", "formHelperTextProps", "fieldProps", "validate"]);
    var classes = useStyles(props);
    // Check context for moment/datefns because formats are different
    var context = React.useContext(pickers_1.MuiPickersContext) || {};
    var utilsName = context.constructor.name;
    if (utilsName === utils.moment) {
        defaultProps.format = "DD.MM.YYYY";
    }
    if (utilsName === utils.dateFns) {
        defaultProps.format = "dd.MM.yyyy";
    }
    return (React.createElement(FormikField_1.FormikField, __assign({ name: name, useField: useField, validate: validate }, fieldProps), function (_a) {
        var field = _a.field, form = _a.form;
        return (React.createElement(React.Fragment, null,
            React.createElement(pickers_1.KeyboardDatePicker, __assign({}, defaultProps, field, { className: classes.datePickerStyles, value: field.value || null, onChange: function (date) {
                    form.setFieldValue(name, date);
                }, error: (0, utils_1.hasError)(name, form, error), helperText: (0, utils_1.getHelperText)(name, form, helperText) }, others))));
    }));
}
exports.FormikDatepicker = FormikDatepicker;
