import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useMemo } from "react";
import { cva } from "class-variance-authority";
import { cn } from "../utils/css";
import { Slot } from "@radix-ui/react-slot";
import { dayjs } from "@orderly.network/utils";
import { firstLetterToUpperCase } from "../utils/string";
import { OrderStatus } from "@orderly.network/types";
const textVariants = cva([], {
    variants: {
        variant: {},
        type: {
            primary: "orderly-text-primary",
            secondary: "orderly-text-secondary",
            tertiary: "orderly-text-tertiary",
            quaternary: "orderly-text-gray-300",
            warning: "orderly-text-warning",
            danger: "orderly-text-danger",
            success: "orderly-text-success",
            buy: "orderly-text-trade-profit",
            sell: "orderly-text-trade-loss"
        }
    }
});
export const Text = (props)=>{
    const { variant, rule, asChildren, type, className, children, symbolElement, surfix, formatString, ...rest } = props;
    const Comp = asChildren ? Slot : "span";
    const content = useMemo(()=>{
        if (typeof children === "undefined") return "--";
        if (typeof rule === "undefined" || rule === "text") return children;
        if (rule === "address") {
            const address = children;
            const [start, end] = props.range ?? [
                6,
                4
            ];
            const reg = new RegExp(`^(.{${start}})(.*)(.{${end}})$`);
            return `${address.replace(reg, "$1...$3")}`;
        }
        if (rule === "date") {
            // return new Date(children as string).toLocaleString();
            return dayjs(children).format(formatString ?? "YYYY-MM-DD HH:mm:ss");
        }
        if (rule === "symbol") {
            const arr = children.split("_");
            if (typeof symbolElement !== "undefined") {
                if (symbolElement === "base") {
                    return arr[1];
                }
                return arr[2];
            }
            return `${arr[1]}-${arr[0]}`;
        }
        if (rule === "status") {
            const status = children;
            if (status === OrderStatus.NEW || status === OrderStatus.OPEN) {
                return "Pending";
            }
            const text = firstLetterToUpperCase(status);
            return text;
        }
        return children;
    }, [
        children,
        rule
    ]);
    const contentWithSurfix = useMemo(()=>{
        if (typeof surfix === "undefined") return content;
        return /*#__PURE__*/ _jsxs("span", {
            className: "orderly-flex orderly-gap-1",
            children: [
                content,
                surfix
            ]
        });
    }, [
        content,
        surfix
    ]);
    return /*#__PURE__*/ _jsx(Comp, {
        ...rest,
        className: cn(textVariants({
            variant,
            type,
            className
        })),
        children: contentWithSurfix
    });
};

//# sourceMappingURL=text.js.map