"use strict";
/*
  This `Link` component is a wrap of the standard
  NextJs `Link` component, with some simple lang
  redirect logic in place.

  If you haven't already, read this issue comment:
  https://github.com/zeit/next.js/issues/2833#issuecomment-414919347

  This component automatically provides this functionality:
  <Link href="/product?slug=something" as="/products/something">

  Wherein `slug` is actually our i18n lang, and it gets
  pulled automatically.

  Very important: if you import `Link` from NextJs directly,
  and not this file, your lang subpath routing will break.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
const react_1 = __importDefault(require("react"));
const prop_types_1 = __importDefault(require("prop-types"));
const link_1 = __importDefault(require("next/link"));
const react_i18next_1 = require("react-i18next");
const utils_1 = require("../utils");
const removeWithTranslationProps = (props) => {
    const strippedProps = Object.assign({}, props);
    delete strippedProps.defaultNS;
    delete strippedProps.i18n;
    delete strippedProps.i18nOptions;
    delete strippedProps.lng;
    delete strippedProps.reportNS;
    delete strippedProps.t;
    delete strippedProps.tReady;
    delete strippedProps.forwardedRef;
    return strippedProps;
};
class Link extends react_1.default.Component {
    render() {
        const { as, children, href, i18n, nextI18NextInternals, ...props } = this.props;
        const { config } = nextI18NextInternals;
        const { language } = i18n;
        if (utils_1.subpathIsRequired(config, language)) {
            const { as: correctedAs, href: correctedHref } = utils_1.lngPathCorrector(config, { as, href }, language);
            return (react_1.default.createElement(link_1.default, Object.assign({ href: correctedHref, as: correctedAs }, removeWithTranslationProps(props)), children));
        }
        return (react_1.default.createElement(link_1.default, Object.assign({ href: href, as: as }, removeWithTranslationProps(props)), children));
    }
}
Link.propTypes = {
    as: prop_types_1.default.string,
    children: prop_types_1.default.node.isRequired,
    href: prop_types_1.default.oneOfType([prop_types_1.default.string, prop_types_1.default.object]).isRequired,
    nextI18NextInternals: prop_types_1.default.shape({
        config: prop_types_1.default.shape({
            defaultLanguage: prop_types_1.default.string.isRequired,
            localeSubpaths: prop_types_1.default.object.isRequired
        }).isRequired
    }).isRequired
};
Link.defaultProps = {
    as: undefined
};
/*
  Usage of `withTranslation` here is just to
  force `Link` to rerender on language change
*/
exports.default = react_i18next_1.withTranslation()(Link);
