"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.ClickToSourceProviderStateless = exports.ClickToSourceProvider = void 0;
var core_1 = require("@chakra-ui/core");
var react_1 = __importStar(require("react"));
var react_dom_1 = __importDefault(require("react-dom"));
var ClickToSourceContext = react_1.createContext({ provider: '' });
function ClickToSourceProvider(_a) {
    var _b = _a.children, children = _b === void 0 ? null : _b;
    var _c = react_1.useState({ provider: '' }), state = _c[0], setState = _c[1];
    var elem = react_1.useState(function () { return document.createElement('div'); })[0];
    react_1.useEffect(function () {
        document.body.appendChild(elem);
    }, []);
    var selectElement = react_1.useMemo(function () { return (react_1["default"].createElement(core_1.Box, { p: '10', position: 'fixed', top: '0', right: '0' },
        react_1["default"].createElement(core_1.Select, { onChange: function (e) {
                var value = e.target.value;
                setState(function (state) { return (__assign(__assign({}, state), { provider: value })); });
            }, fontWeight: 500, value: state.provider, opacity: 0.8, w: 'auto' },
            react_1["default"].createElement("option", { value: '', children: 'click to source disabled' }),
            react_1["default"].createElement("option", { value: 'vscode', children: 'click to source on Vscode' })))); }, [setState, state]);
    return (react_1["default"].createElement(ClickToSourceProviderStateless, { value: state, onChange: function (x) { return setState(x); } },
        elem && react_dom_1["default"].createPortal(selectElement, elem),
        children));
}
exports.ClickToSourceProvider = ClickToSourceProvider;
function ClickToSourceProviderStateless(_a) {
    var _b = _a.value, value = _b === void 0 ? { provider: '' } : _b, onChange = _a.onChange, _c = _a.className, className = _c === void 0 ? '' : _c, _d = _a.children, children = _d === void 0 ? null : _d, rest = __rest(_a, ["value", "onChange", "className", "children"]);
    // const [state, setState] = useState<ClickToSourceState>(value)
    var enabled = Boolean(value.provider);
    var ref = react_1.useRef();
    var disable = react_1.useCallback(function () {
        onChange(__assign(__assign({}, value), { provider: '' }));
    }, [onChange]);
    var onClickToSource = react_1.useCallback(function onClickToSource(e) {
        // e.preventDefault()
        e.stopPropagation();
        disable();
        var target = e.currentTarget;
        var filename = target.getAttribute('data-vitro-filename');
        // TODO filename is a path realtive to the root of the repository, this path can be used on vscode or on github using different path.join
        var lineNumber = target.getAttribute('data-vitro-line');
        if (!filename) {
            console.warn("no filename found among " + [target.attributes]
                .map(function (x) { return x.name + "='" + x.value + "'"; })
                .join(', '));
            return;
        }
        if (!lineNumber) {
            console.warn("no line number found for " + target + " among " + target.attributes);
            return;
        }
        window.location.href = "vscode://file" + filename + ":" + lineNumber;
    }, []);
    react_1.useEffect(function () {
        var root = ref.current;
        if (!root) {
            console.error(new Error("no root ref"));
        }
        if (enabled) {
            var elements = Array.from(root.querySelectorAll('.click-to-source-enabled *[data-vitro-filename]'));
            elements.forEach(function (elem) {
                elem.addEventListener('click', onClickToSource);
            });
        }
        else {
            var elements = Array.from(root.querySelectorAll('*[data-vitro-filename]'));
            elements.forEach(function (elem) {
                elem.removeEventListener('click', onClickToSource);
            });
        }
    }, [enabled]);
    var enabledClassName = enabled ? 'click-to-source-enabled' : '';
    return (react_1["default"].createElement(ClickToSourceContext.Provider, { value: value },
        react_1["default"].createElement("div", __assign({ className: className
                ? className + " " + enabledClassName
                : enabledClassName, ref: ref }, rest), children)));
}
exports.ClickToSourceProviderStateless = ClickToSourceProviderStateless;
//# sourceMappingURL=ClickToSource.js.map