"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = (0, tslib_1.__importStar)(require("react"));
var classnames_1 = (0, tslib_1.__importDefault)(require("../../utils/classnames"));
require("./index.css");
// 所有绝对地址直接交由浏览器，新开窗口交由浏览器
// 当前窗口相对地址交给平台
function Link(_a) {
    var _b, e_1, _c;
    var _d = _a.content, content = _d === void 0 ? '这是一个链接' : _d, _e = _a.url, url = _e === void 0 ? '' : _e, _f = _a.events, events = _f === void 0 ? {} : _f, style = _a.style, className = _a.className, contentSlot = _a.contentSlot, _g = _a.params, params = _g === void 0 ? [] : _g;
    url = url.trim();
    var cls = (0, classnames_1.default)((_b = {
            'weda-link': true,
            'weda-ui': true
        },
        _b[className] = className,
        _b));
    // URL 构造函数不支持 '//xxx.com' 的格式
    if (url.indexOf('//') === 0) {
        url = location.protocol + url;
    }
    // "proto:" 或 "//" 起头的是绝对路径
    var isAbosolutePath = /^(?:[a-z]+:)/i.test(url);
    var base, path, href, paramObj = {}, urlObj;
    if (!isAbosolutePath) {
        // 相对地址使用站位域名，不实际使用
        base =
            location.origin && location.origin !== 'null'
                ? location.origin
                : 'https://we.da';
    }
    try {
        urlObj = new URL(url, base);
        params.forEach(function (x) {
            urlObj.searchParams.append(x.key, x.value);
        });
        href = urlObj.href;
        path = urlObj.pathname;
        try {
            for (var _h = (0, tslib_1.__values)(urlObj.searchParams), _j = _h.next(); !_j.done; _j = _h.next()) {
                var _k = (0, tslib_1.__read)(_j.value, 2), key = _k[0], value = _k[1];
                paramObj[key] = value;
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
            }
            finally { if (e_1) throw e_1.error; }
        }
        // 相对地址去掉站位域名
        if (!isAbosolutePath) {
            href = urlObj.pathname + urlObj.search + urlObj.hash;
            href = href.replace(/^\//, '');
        }
    }
    catch (e) {
        // 非法 url
        isAbosolutePath = true;
        href = url;
        path = '';
        paramObj = params.reduce(function (p, x) {
            p[x.key] = x.value;
            return p;
        }, {});
    }
    return (React.createElement("a", { style: (0, tslib_1.__assign)({}, style), className: cls, href: href, onClick: function (e) {
            events.tap && events.tap({}, { originEvent: e });
            if (!e.defaultPrevented) {
                if (!isAbosolutePath) {
                    e.preventDefault();
                    app.navigateTo({
                        pageId: path,
                        packageName: '',
                        mode: 'weDa',
                        params: paramObj,
                    });
                }
            }
        }, rel: "noreferrer" },
        contentSlot && React.createElement("div", { className: "weda-link-slot" }, contentSlot),
        content));
}
exports.default = Link;
//# sourceMappingURL=index.js.map