import React from 'react';

/**
 * rtl.ts — Right-to-left (RTL) UI support for editors.
 *
 * Provides RTL direction context, CSS logical properties,
 * and layout utilities for RTL languages (Arabic, Hebrew, etc.).
 */
// ── Context ─────────────────────────────────────────────────────
const defaultRTLConfig = {
    direction: "ltr",
    mirrorToolbar: false,
    mirrorPanels: false,
};
const RTLContext = React.createContext(defaultRTLConfig);
/** Provider for RTL configuration. Wraps editors to enable RTL support. */
const NiceRTLProvider = ({ direction = "ltr", mirrorToolbar = true, mirrorPanels = true, children }) => {
    const config = React.useMemo(() => ({
        direction,
        mirrorToolbar: direction === "rtl" ? mirrorToolbar : false,
        mirrorPanels: direction === "rtl" ? mirrorPanels : false,
    }), [direction, mirrorToolbar, mirrorPanels]);
    return React.createElement(RTLContext.Provider, { value: config }, React.createElement("div", {
        dir: direction,
        style: { direction },
    }, children));
};
/** Hook to access RTL configuration. */
function useRTL() {
    return React.useContext(RTLContext);
}
/** Hook that returns true if the current direction is RTL. */
function useIsRTL() {
    return React.useContext(RTLContext).direction === "rtl";
}
// ── CSS Helpers ─────────────────────────────────────────────────
/** Convert physical CSS properties to logical equivalents for RTL awareness. */
function logicalStyles(direction, styles) {
    if (direction === "ltr")
        return styles;
    const mapped = { ...styles };
    // Swap left/right margins
    if ("marginLeft" in styles || "marginRight" in styles) {
        mapped.marginLeft = styles.marginRight;
        mapped.marginRight = styles.marginLeft;
    }
    // Swap left/right padding
    if ("paddingLeft" in styles || "paddingRight" in styles) {
        mapped.paddingLeft = styles.paddingRight;
        mapped.paddingRight = styles.paddingLeft;
    }
    // Swap left/right positioning
    if ("left" in styles || "right" in styles) {
        mapped.left = styles.right;
        mapped.right = styles.left;
    }
    // Swap borderRadius corners
    if ("borderTopLeftRadius" in styles || "borderTopRightRadius" in styles) {
        mapped.borderTopLeftRadius = styles.borderTopRightRadius;
        mapped.borderTopRightRadius = styles.borderTopLeftRadius;
    }
    if ("borderBottomLeftRadius" in styles || "borderBottomRightRadius" in styles) {
        mapped.borderBottomLeftRadius = styles.borderBottomRightRadius;
        mapped.borderBottomRightRadius = styles.borderBottomLeftRadius;
    }
    // Swap text-align
    if (styles.textAlign === "left")
        mapped.textAlign = "right";
    else if (styles.textAlign === "right")
        mapped.textAlign = "left";
    return mapped;
}
/** Get panel flex direction based on RTL. */
function rtlFlexDirection(direction) {
    return direction === "rtl" ? "row-reverse" : "row";
}
/** Detect direction from document or browser locale. */
function detectDirection() {
    var _a, _b;
    if (typeof document !== "undefined") {
        const dir = document.documentElement.getAttribute("dir");
        if (dir === "rtl")
            return "rtl";
    }
    if (typeof navigator !== "undefined") {
        const lang = (_b = (_a = navigator.language) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : "";
        const rtlLangs = ["ar", "he", "fa", "ur", "ps", "sd", "yi", "ku"];
        if (rtlLangs.some((l) => lang.startsWith(l)))
            return "rtl";
    }
    return "ltr";
}

export { NiceRTLProvider, detectDirection, logicalStyles, rtlFlexDirection, useIsRTL, useRTL };
//# sourceMappingURL=rtl.js.map
