"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.DateFnsLocalizedComponent = void 0;
var tslib_1 = require("tslib");
var core_1 = require("@blueprintjs/core");
var dateFnsLocaleUtils_1 = require("../common/dateFnsLocaleUtils");
/**
 * Abstract component which accepts a date-fns locale prop and loads the corresponding `Locale` object as necessary.
 *
 * Currently used by DatePicker3, DateRangePicker3, and DateRangeInput3, but we would ideally migrate to the
 * `useDateFnsLocale()` hook once those components are refactored into functional components.
 */
var DateFnsLocalizedComponent = /** @class */ (function (_super) {
    tslib_1.__extends(DateFnsLocalizedComponent, _super);
    function DateFnsLocalizedComponent() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        // Keeping track of `isMounted` state is generally considered an anti-pattern, but since there is no way to
        // cancel/abort dyanmic ES module `import()` calls to load the date-fns locale, this is the best way to avoid
        // setting state on an unmounted component, which creates noise in the console (especially while running tests).
        // N.B. this cannot be named `isMounted` because that conflicts with a React internal property.
        _this.isComponentMounted = false;
        return _this;
    }
    // HACKHACK: type fix for setState which does not accept partial state objects in our version of
    // @types/react (v16.14.x)
    DateFnsLocalizedComponent.prototype.setState = function (nextStateOrAction, callback) {
        if (typeof nextStateOrAction === "function") {
            _super.prototype.setState.call(this, nextStateOrAction, callback);
        }
        else {
            _super.prototype.setState.call(this, nextStateOrAction);
        }
    };
    DateFnsLocalizedComponent.prototype.componentDidMount = function () {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.isComponentMounted = true;
                        return [4 /*yield*/, this.loadLocale(this.props.locale)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    DateFnsLocalizedComponent.prototype.componentDidUpdate = function (prevProps) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!(this.props.locale !== prevProps.locale)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.loadLocale(this.props.locale)];
                    case 1:
                        _a.sent();
                        _a.label = 2;
                    case 2: return [2 /*return*/];
                }
            });
        });
    };
    DateFnsLocalizedComponent.prototype.componentWillUnmount = function () {
        this.isComponentMounted = false;
    };
    DateFnsLocalizedComponent.prototype.loadLocale = function (localeOrCode) {
        var _a, _b;
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var loader, locale;
            return tslib_1.__generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        if (localeOrCode === undefined) {
                            return [2 /*return*/];
                        }
                        else if (((_a = this.state.locale) === null || _a === void 0 ? void 0 : _a.code) === localeOrCode) {
                            return [2 /*return*/];
                        }
                        if (!(typeof localeOrCode === "string")) return [3 /*break*/, 2];
                        loader = (_b = this.props.dateFnsLocaleLoader) !== null && _b !== void 0 ? _b : dateFnsLocaleUtils_1.loadDateFnsLocale;
                        return [4 /*yield*/, loader(localeOrCode)];
                    case 1:
                        locale = _c.sent();
                        if (this.isComponentMounted) {
                            this.setState({ locale: locale });
                        }
                        return [3 /*break*/, 3];
                    case 2:
                        this.setState({ locale: localeOrCode });
                        _c.label = 3;
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    return DateFnsLocalizedComponent;
}(core_1.AbstractPureComponent));
exports.DateFnsLocalizedComponent = DateFnsLocalizedComponent;
//# sourceMappingURL=dateFnsLocalizedComponent.js.map