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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { memo, useState, useEffect } from "react";
import { makeStyles } from "../theme";
import SyntaxHighLighter from "react-syntax-highlighter/dist/esm/prism-async-light";
import { useStateRef } from "powerhooks/useStateRef";
import CopyAllIcon from "@mui/icons-material/CopyAll";
import { Text } from "../theme";
import { useConstCallback } from "powerhooks/useConstCallback";
import { useDomRect } from "powerhooks/useDomRect";
import { getTheme } from "./getTheme";
export var GlCodeBlock = memo(function (props) {
    var text = props.text, className = props.className, _a = props.hasShadow, hasShadow = _a === void 0 ? true : _a, _b = props.programingLanguage, language = _b === void 0 ? "typescript" : _b, _c = props.showLineNumbers, showLineNumbers = _c === void 0 ? false : _c, _d = props.hasCopyButton, hasCopyButton = _d === void 0 ? false : _d, _e = props.copiedMessage, copiedMessage = _e === void 0 ? "Copied !" : _e, _f = props.hasBorderRadius, hasBorderRadius = _f === void 0 ? true : _f, _g = props.darkTheme, darkThemeUserInput = _g === void 0 ? "atom-dark" : _g, _h = props.lightTheme, lightThemeUserInput = _h === void 0 ? "solarizedlight" : _h;
    var ref = useStateRef(null);
    var _j = __read(useState(undefined), 2), backgroundColor = _j[0], setBackgroundColor = _j[1];
    var _k = __read(useState(false), 2), isCopiedMessageShowing = _k[0], setIsCopiedMessageShowing = _k[1];
    var _l = __read(useState(false), 2), isMouseDown = _l[0], setIsMouseDown = _l[1];
    var _m = useDomRect(), copiedTextRef = _m.ref, copiedTextWidth = _m.domRect.width;
    var copyCode = useConstCallback(function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (text === undefined) {
                        return [2 /*return*/];
                    }
                    navigator.clipboard.writeText(text);
                    setIsMouseDown(false);
                    setIsCopiedMessageShowing(true);
                    return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
                case 1:
                    _a.sent();
                    setIsCopiedMessageShowing(false);
                    return [2 /*return*/];
            }
        });
    }); });
    var onMouseDown = useConstCallback(function () {
        if (isMouseDown) {
            return;
        }
        setIsMouseDown(true);
    });
    var _o = useStyles({
        hasShadow: hasShadow,
        backgroundColor: backgroundColor,
        isCopiedMessageShowing: isCopiedMessageShowing,
        isMouseDown: isMouseDown,
        hasBorderRadius: hasBorderRadius,
        copiedTextWidth: copiedTextWidth,
    }, { props: props }), classes = _o.classes, cx = _o.cx, theme = _o.theme;
    var _p = __read(useState(), 2), lightTheme = _p[0], setLightTheme = _p[1];
    var _q = __read(useState(), 2), darkTheme = _q[0], setDarkTheme = _q[1];
    useEffect(function () {
        getTheme({
            "dark": darkThemeUserInput,
            "light": lightThemeUserInput,
        }).then(function (theme) {
            var dark = theme.dark, light = theme.light;
            setLightTheme(light);
            setDarkTheme(dark);
        });
    }, [darkThemeUserInput, lightThemeUserInput]);
    useEffect(function () {
        var _a;
        if (!((_a = ref.current) === null || _a === void 0 ? void 0 : _a.firstElementChild)) {
            return;
        }
        setBackgroundColor(window.getComputedStyle(ref.current.firstElementChild)
            .backgroundColor);
    }, [ref.current, theme.isDarkModeEnabled, darkTheme, lightTheme]);
    return (_jsxs("div", __assign({ ref: ref, className: cx(classes.root, className) }, { children: [_jsx(SyntaxHighLighter, __assign({ language: language, showInlineLineNumbers: showLineNumbers, style: theme.isDarkModeEnabled ? darkTheme : lightTheme }, { children: text })), hasCopyButton ? (_jsxs("div", __assign({ className: classes.copyWrapper }, { children: [_jsx(Text, __assign({ ref: copiedTextRef, className: classes.copiedText, typo: "caption" }, { children: copiedMessage })), _jsx(CopyAllIcon, { onMouseUp: copyCode, onMouseDown: onMouseDown, className: classes.copyButton })] }))) : undefined] })));
});
var useStyles = makeStyles()(function (theme, _a) {
    var hasShadow = _a.hasShadow, backgroundColor = _a.backgroundColor, isCopiedMessageShowing = _a.isCopiedMessageShowing, isMouseDown = _a.isMouseDown, hasBorderRadius = _a.hasBorderRadius, copiedTextWidth = _a.copiedTextWidth;
    return ({
        "root": {
            "display": "flex",
            "opacity": backgroundColor === undefined ? 0 : 1,
            backgroundColor: backgroundColor,
            "borderRadius": hasBorderRadius ? theme.borderRadius : undefined,
            "boxShadow": hasShadow ? theme.customShadow : undefined,
            "overflow": "hidden",
            "justifyContent": "space-between",
        },
        "copyButton": __assign({ "fill": isMouseDown
                ? theme.colors.palette.focus.main
                : theme.colors.useCases.typography.textSecondary, "cursor": "pointer" }, (function () {
            var value = theme.spacing(2);
            return {
                "marginTop": value,
                "marginRight": value,
            };
        })()),
        "copyWrapper": {
            "position": "relative",
        },
        "copiedText": {
            "transition": "opacity 200ms",
            "opacity": isCopiedMessageShowing ? 1 : 0,
            "position": "absolute",
            "backgroundColor": theme.colors.palette.focus.main,
            "padding": theme.spacing(2),
            "borderRadius": "30px",
            "whiteSpace": "nowrap",
            "top": theme.spacing(1),
            "left": -(copiedTextWidth + theme.spacing(2)),
            "color": theme.colors.palette.light.main,
            "pointerEvents": "none",
        },
    });
});
//# sourceMappingURL=GlCodeBlock.js.map