"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_native_1 = require("react-native");
const getComponent_1 = require("../../getComponent");
const react_1 = tslib_1.__importDefault(require("react"));
const TouchableItem_1 = require("../TouchableItem");
/**
 * 🔗 Link
 */
class Link extends react_1.default.PureComponent {
    handlePress(event, navigation) {
        const { method, routeName, path, params, replace } = this.props;
        if (!event.defaultPrevented) {
            event.preventDefault();
            let fn = navigation.navigate;
            if (method) {
                fn = navigation[method];
            }
            if (replace === true) {
                fn = navigation.replace;
            }
            if (typeof routeName === 'string') {
                fn(routeName, params);
            }
            else if (typeof path === 'string') {
                fn({ path }, params);
            }
        }
    }
    render() {
        const _a = this.props, { component: Component, routeName, path, params, onPress, replace } = _a, rest = tslib_1.__rest(_a, ["component", "routeName", "path", "params", "onPress", "replace"]);
        return Component ? (react_1.default.createElement(getComponent_1.NavigationActions, null, (navigation) => {
            const onPressDefault = (e) => this.handlePress(e, navigation);
            return (react_native_1.Platform.OS === 'web' && !!path && path !== '')
                ? react_1.default.createElement("a", Object.assign({ href: path, onClick: onPress || onPressDefault }, this.props))
                : react_1.default.createElement(Component, Object.assign({}, rest, { onPress: onPress || onPressDefault }));
        })) : null;
    }
}
Link.defaultProps = {
    component: TouchableItem_1.TouchableItem,
    replace: false,
};
exports.Link = Link;
