"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const prop_types_1 = __importDefault(require("prop-types"));
const react_2 = require("@lingui/react");
class I18nLoader extends react_1.default.Component {
    constructor() {
        super(...arguments);
        this.state = {
            catalogs: {},
        };
        this.loadCatalog = (language) => __awaiter(this, void 0, void 0, function* () {
            const catalog = yield Promise.resolve().then(() => __importStar(require(/* webpackMode: "lazy", webpackChunkName: "i18n-[index]" */ `@kudoo/locale/${language}/messages.js`)));
            this.setState(state => ({
                catalogs: Object.assign({}, state.catalogs, { [language]: catalog }),
            }));
        });
    }
    componentDidMount() {
        this.loadCatalog(this.props.language);
    }
    shouldComponentUpdate(nextProps, nextState) {
        const { language } = nextProps;
        const { catalogs } = nextState;
        if (language !== this.props.language && !catalogs[language]) {
            this.loadCatalog(language);
            return false;
        }
        return true;
    }
    render() {
        const { children, language } = this.props;
        const { catalogs } = this.state;
        // Skip rendering when catalog isn't loaded.
        if (!catalogs[language]) {
            return null;
        }
        return (react_1.default.createElement(react_2.I18nProvider, { language: language, catalogs: catalogs, missing: "\uD83D\uDEA8" }, children));
    }
}
I18nLoader.propTypes = {
    language: prop_types_1.default.string,
    children: prop_types_1.default.node,
};
I18nLoader.defaultProps = {
    language: 'en_AU',
};
exports.I18nLoader = I18nLoader;
exports.default = I18nLoader;
//# sourceMappingURL=index.js.map