"use strict";
/*
 * Copyright 2022 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultValueAdapter = exports.valueAdapter = exports.onChangeAdapter = void 0;
var date_fns_1 = require("date-fns");
var datetime_1 = require("@blueprintjs/datetime");
/**
 * `onChange` prop adapter for automated DateInput -> DateInput2 migration in @blueprintjs/datetime2 v0.x.
 *
 * Note that we exclude `undefined` from the input & output types since we expect the callback to be defined
 * if this adapter is used.
 *
 * @param handler DateInput onChange handler
 * @returns DateInput2 onChange handler
 */
function onChangeAdapter(handler) {
    if (handler === undefined) {
        return noOp;
    }
    var tz = datetime_1.TimezoneUtils.getCurrentTimezone();
    return function (newDate, isUserChange) { var _a; return handler((_a = datetime_1.TimezoneUtils.getDateObjectFromIsoString(newDate, tz)) !== null && _a !== void 0 ? _a : null, isUserChange); };
}
exports.onChangeAdapter = onChangeAdapter;
/**
 * `value` prop adapter for automated DateInput -> DateInput2 migration in @blueprintjs/datetime2 v0.x.
 *
 * @param value DateInput value
 * @param timePrecision (optional) DateInput timePrecision
 * @returns DateInput2 value
 */
function valueAdapter(value, timePrecision) {
    if (value == null || !(0, date_fns_1.isValid)(value)) {
        return null;
    }
    return convertDateToDateString(value, timePrecision);
}
exports.valueAdapter = valueAdapter;
/**
 * Adapter for automated DateInput -> DateInput2 migration in @blueprintjs/datetime2 v0.x.
 *
 * @param defaultValue DateInput value
 * @param timePrecision (optional) DateInput timePrecision
 * @returns DateInput2 value
 */
function defaultValueAdapter(defaultValue, timePrecision) {
    if (defaultValue === undefined || !(0, date_fns_1.isValid)(defaultValue)) {
        return undefined;
    }
    return convertDateToDateString(defaultValue, timePrecision);
}
exports.defaultValueAdapter = defaultValueAdapter;
function convertDateToDateString(date, timePrecision) {
    var tz = datetime_1.TimezoneUtils.getCurrentTimezone();
    var inferredTimePrecision = date.getMilliseconds() !== 0
        ? datetime_1.TimePrecision.MILLISECOND
        : date.getSeconds() !== 0
            ? datetime_1.TimePrecision.SECOND
            : date.getMinutes() !== 0
                ? datetime_1.TimePrecision.MINUTE
                : undefined;
    return datetime_1.TimezoneUtils.getIsoEquivalentWithUpdatedTimezone(date, tz, timePrecision !== null && timePrecision !== void 0 ? timePrecision : inferredTimePrecision);
}
function noOp() {
    // nothing
}
//# sourceMappingURL=dateInput2MigrationUtils.js.map