"use strict";
/*
 * Copyright 2023 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.useDateFnsLocale = exports.loadDateFnsLocale = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var core_1 = require("@blueprintjs/core");
/**
 * Lazy-loads a date-fns locale for use in a datetime class component.
 */
function loadDateFnsLocale(localeCode) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var localeModule, _a;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    _b.trys.push([0, 2, , 3]);
                    return [4 /*yield*/, Promise.resolve("".concat(
                        /* webpackChunkName: "date-fns-locale-[request]" */
                        "date-fns/locale/".concat(localeCode, "/index.js"))).then(function (s) { return tslib_1.__importStar(require(s)); })];
                case 1:
                    localeModule = _b.sent();
                    return [2 /*return*/, localeModule.default];
                case 2:
                    _a = _b.sent();
                    if (!core_1.Utils.isNodeEnv("production")) {
                        console.error("[Blueprint] Could not load \"".concat(localeCode, "\" date-fns locale, please check that this locale code is supported: https://github.com/date-fns/date-fns/tree/main/src/locale"));
                    }
                    return [2 /*return*/, undefined];
                case 3: return [2 /*return*/];
            }
        });
    });
}
exports.loadDateFnsLocale = loadDateFnsLocale;
/**
 * Lazy-loads a date-fns locale for use in a datetime function component.
 */
function useDateFnsLocale(localeOrCode, dateFnsLocaleLoader) {
    if (dateFnsLocaleLoader === void 0) { dateFnsLocaleLoader = loadDateFnsLocale; }
    // make sure to set the locale correctly on first render if it is available
    var _a = React.useState(typeof localeOrCode === "object" ? localeOrCode : undefined), locale = _a[0], setLocale = _a[1];
    React.useEffect(function () {
        setLocale(function (prevLocale) {
            if (typeof localeOrCode === "string") {
                dateFnsLocaleLoader(localeOrCode).then(setLocale);
                // keep the current locale for now, it will be updated async
                return prevLocale;
            }
            else {
                return localeOrCode;
            }
        });
    }, [dateFnsLocaleLoader, localeOrCode]);
    return locale;
}
exports.useDateFnsLocale = useDateFnsLocale;
//# sourceMappingURL=dateFnsLocaleUtils.js.map