import * as tslib_1 from "tslib";
import { filterMissingMarkup, findFilterMissing } from '../missing';
import ADialog from './ADialog';
import { updateFilterState, uniqueId } from './utils';
import { isDummyDateFilter, noDateFilter, shiftFilterDateDay } from '../../model/internalDate';
import { timeFormat } from 'd3-time-format';
/** @internal */
var DateFilterDialog = /** @class */ (function (_super) {
    tslib_1.__extends(DateFilterDialog, _super);
    function DateFilterDialog(column, dialog) {
        var _this = _super.call(this, dialog) || this;
        _this.column = column;
        return _this;
    }
    DateFilterDialog.prototype.updateFilter = function (filter) {
        updateFilterState(this.attachment, this.column, filter != null && !isDummyDateFilter(filter));
        this.column.setFilter(filter);
    };
    DateFilterDialog.prototype.reset = function () {
        this.forEach('input[type=date]', function (n) { return n.value = ''; });
        this.forEach('input[type=checkbox]', function (n) { return n.checked = false; });
        this.updateFilter(null);
    };
    DateFilterDialog.prototype.submit = function () {
        var filterMissing = findFilterMissing(this.node).checked;
        var from = this.findInput('input[name="from"]').valueAsDate;
        var to = this.findInput('input[name="to"]').valueAsDate;
        this.updateFilter({
            filterMissing: filterMissing,
            min: from == null ? -Infinity : shiftFilterDateDay(from.getTime(), 'min'),
            max: to == null ? +Infinity : shiftFilterDateDay(to.getTime(), 'max')
        });
        return true;
    };
    DateFilterDialog.prototype.build = function (node) {
        var _this = this;
        var act = this.column.getFilter() || noDateFilter();
        var id = uniqueId(this.dialog.idPrefix);
        var f = timeFormat('%Y-%m-%d');
        node.insertAdjacentHTML('beforeend', "\n    <label for=\"" + id + "F\">From: </label>\n    <input type=\"date\" id=\"" + id + "F\" name=\"from\" placeholder=\"From...\" autofocus value=\"" + (isFinite(act.min) ? f(new Date(act.min)) : '') + "\">\n    <label for=\"" + id + "T\">To: </label>\n    <input type=\"date\" id=\"" + id + "T\" name=\"to\" placeholder=\"To...\" value=\"" + (isFinite(act.max) ? f(new Date(act.max)) : '') + "\">\n    " + filterMissingMarkup(act.filterMissing));
        var update = function () {
            _this.submit();
        };
        this.forEach('input', function (n) { return n.onchange = update; });
    };
    return DateFilterDialog;
}(ADialog));
export default DateFilterDialog;
//# sourceMappingURL=DateFilterDialog.js.map