"use client";
'use strict';

var tslib_es6 = require('../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.js');
var jsxRuntime = require('react/jsx-runtime');
var Image = require('next/image');
var React = require('react');
var lib = require('@windrun-huaiin/base-ui/lib');
var utils = require('@windrun-huaiin/lib/utils');
var snakeLoadingFrame = require('./snake-loading-frame.js');

var _a;
const ENV_DELAY_ENABLED = process.env.NEXT_PUBLIC_DELAYED_IMG_ENABLED === "true";
const rawDelaySeconds = (_a = process.env.NEXT_PUBLIC_DELAYED_IMG_SECONDS) !== null && _a !== void 0 ? _a : "0";
const parsedDelaySeconds = Number(rawDelaySeconds);
const ENV_DELAY_MS = Number.isFinite(parsedDelaySeconds) && parsedDelaySeconds > 0
    ? parsedDelaySeconds * 1000
    : 0;
function DelayedImg(_a) {
    var { alt, wrapperClassName, placeholderClassName, className, onLoad } = _a, imageProps = tslib_es6.__rest(_a, ["alt", "wrapperClassName", "placeholderClassName", "className", "onLoad"]);
    const shouldDelay = ENV_DELAY_ENABLED && ENV_DELAY_MS > 0;
    const [isMounted, setIsMounted] = React.useState(!shouldDelay);
    const [isLoaded, setIsLoaded] = React.useState(false);
    React.useEffect(() => {
        if (!shouldDelay || isMounted) {
            return;
        }
        const timer = window.setTimeout(() => {
            setIsMounted(true);
        }, ENV_DELAY_MS);
        return () => window.clearTimeout(timer);
    }, [isMounted, shouldDelay]);
    return (jsxRuntime.jsxs("div", { className: utils.cn("relative", wrapperClassName), children: [(!isMounted || !isLoaded) && (jsxRuntime.jsx(snakeLoadingFrame.SnakeLoadingFrame, { shape: "rounded-rect", loading: true, themeColor: lib.themeSvgIconColor, strokeWidth: 3, className: utils.cn("absolute inset-0 rounded-[inherit] border shadow-sm bg-white/70 dark:bg-white/5", lib.themeBgColor, placeholderClassName), contentClassName: "h-full w-full", children: jsxRuntime.jsx("div", { "aria-hidden": "true", className: "absolute inset-0 rounded-[inherit] bg-white/20 dark:bg-white/0" }) })), isMounted && (jsxRuntime.jsx(Image, Object.assign({}, imageProps, { alt: alt, onLoad: (event) => {
                    setIsLoaded(true);
                    onLoad === null || onLoad === void 0 ? void 0 : onLoad(event);
                }, className: utils.cn("transition duration-300", isLoaded ? "opacity-100" : "opacity-0", className) })))] }));
}

exports.DelayedImg = DelayedImg;
