"use strict";
var __assign = (this && this.__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;
};
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)
            t[p[i]] = s[p[i]];
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var assign = require("lodash/assign");
var memoize = require("lodash/memoize");
var moment = require("moment");
var React = require("react");
var utils = require("../../utils");
var Input_1 = require("../Input");
var getDefaultDate = function () {
    var now = new Date();
    var day = ('0' + now.getDate()).slice(-2);
    var month = ('0' + (now.getMonth() + 1)).slice(-2);
    var today = now.getFullYear() + '-' + month + '-' + day;
    return today + "T00:00";
};
var normalizeDateTime = memoize(function (timestamp) {
    return moment(timestamp)
        .utc()
        .format();
});
exports.operators = {
    is: {
        getLabel: function (_s) { return 'is'; },
    },
    is_before: {
        getLabel: function (_s) { return 'is before'; },
    },
    is_after: {
        getLabel: function (_s) { return 'is after'; },
    },
};
exports.decodeFilter = function (filter) {
    if (!filter.properties) {
        return null;
    }
    var keys = Object.keys(filter.properties);
    if (!keys.length) {
        return null;
    }
    var field = keys[0];
    var operator = filter.title;
    var value;
    if (operator === 'is') {
        value = filter.properties[field].const;
    }
    else if (operator === 'is_before') {
        value = filter.properties[field].formatMaximum;
    }
    else if (operator === 'is_after') {
        value = filter.properties[field].formatMinimum;
    }
    else {
        return null;
    }
    return {
        field: field,
        operator: operator,
        value: value,
    };
};
exports.createFilter = function (field, operator, value, schema) {
    var title = schema.title;
    var base = {
        $id: utils.randomString(),
        title: operator,
        description: (title || field) + " " + exports.operators[operator].getLabel(schema) + " " + value,
        type: 'object',
    };
    var normalizedValue = normalizeDateTime(value);
    if (operator === 'is') {
        return assign(base, {
            properties: (_a = {},
                _a[field] = {
                    type: 'string',
                    format: 'date-time',
                    const: normalizedValue,
                },
                _a),
            required: [field],
        });
    }
    if (operator === 'is_before') {
        return assign(base, {
            properties: (_b = {},
                _b[field] = {
                    type: 'string',
                    format: 'date-time',
                    formatMaximum: normalizedValue,
                },
                _b),
            required: [field],
        });
    }
    if (operator === 'is_after') {
        return assign(base, {
            properties: (_c = {},
                _c[field] = {
                    type: 'string',
                    format: 'date-time',
                    formatMinimum: normalizedValue,
                },
                _c),
            required: [field],
        });
    }
    return base;
    var _a, _b, _c;
};
exports.Edit = function (_a) {
    var value = _a.value, onUpdate = _a.onUpdate, props = __rest(_a, ["value", "onUpdate"]);
    if (!value) {
        onUpdate(getDefaultDate());
    }
    return (React.createElement(Input_1.default, __assign({}, props, { type: "datetime-local", value: value, onChange: function (e) {
            return onUpdate(e.currentTarget.value);
        } })));
};
