// components/empty/empty.ts
type SceneConfig = {
    title: string;
    description: string;
    icon: string;
    buttonText?: string;
    action?: string;
    imageStyle?: string;
};

const SCENE_MAP: Record<string, SceneConfig> = {
    loadFail: {
        title: "加载失败",
        description: "请点击刷新重新加载试试~",
        icon: "./assets/load-failed.png",
        buttonText: "刷新",
        action: "refresh",
        imageStyle: "width: 328rpx; height: 392rpx;"
    },
    noDynamic: {
        title: "暂无动态",
        description: "这个地方空空如也~",
        icon: "./assets/no-dynamic.png",
        imageStyle: "width: 260rpx; height: 378rpx;"
    },
    noMessage: {
        title: "暂无消息",
        description: "暂无新的消息",
        icon: "./assets/no-message.png",
        imageStyle: "width: 344rpx; height: 376rpx;"
    },
    noAddress: {
        title: "暂无地址",
        description: "快去添加一个吧～",
        icon: "./assets/no-address.png",
        buttonText: "添加地址",
        action: "addAddress",
        imageStyle: "width: 264rpx; height: 392rpx;"
    },
    noArchive: {
        title: "暂无档案信息",
        description: "去添加宝贝的成长档案吧~",
        icon: "./assets/no-archive.png",
        imageStyle: "width: 388rpx; height: 408rpx;"
    }
};

const DEFAULT_TITLE = "什么都没搜到";
const DEFAULT_ICON = "../../assets/img/empty.svg";

Component({
    properties: {
        scene: {
            type: String,
            value: "",
            observer: "syncScene"
        },
        title: {
            type: String,
            value: DEFAULT_TITLE,
            observer: "syncScene"
        },
        description: {
            type: String,
            value: "",
            observer: "syncScene"
        },
        icon: {
            type: String,
            value: DEFAULT_ICON,
            observer: "syncScene"
        },
        buttonText: {
            type: String,
            value: "",
            observer: "syncScene"
        },
        emptyStyle: {
            type: String,
            value: ""
        },
        imageStyle: {
            type: String,
            value: "",
            observer: "syncScene"
        }
    },

    data: {
        sceneMode: false,
        resolvedTitle: "",
        resolvedDescription: "",
        resolvedIcon: "",
        resolvedButtonText: "",
        resolvedAction: "",
        resolvedImageStyle: ""
    },

    lifetimes: {
        attached() {
            (this as any).syncScene();
        }
    },

    methods: {
        syncScene() {
            const that = this as any;
            const scene = (that.data.scene || "").trim();
            const sceneConfig = scene ? SCENE_MAP[scene] : undefined;
            const title = that.data.title || "";
            const description = that.data.description || "";
            const icon = that.data.icon || "";
            const buttonText = that.data.buttonText || "";
            const imageStyle = that.data.imageStyle || "";

            that.setData({
                sceneMode: !!sceneConfig,
                resolvedTitle: sceneConfig && title === DEFAULT_TITLE ? sceneConfig.title : (title || sceneConfig?.title || DEFAULT_TITLE),
                resolvedDescription: sceneConfig && !description ? sceneConfig.description : description,
                resolvedIcon: sceneConfig && icon === DEFAULT_ICON ? sceneConfig.icon : (icon || sceneConfig?.icon || DEFAULT_ICON),
                resolvedButtonText: sceneConfig && !buttonText ? (sceneConfig.buttonText || "") : buttonText,
                resolvedAction: sceneConfig?.action || "",
                resolvedImageStyle: sceneConfig && !imageStyle ? (sceneConfig.imageStyle || "") : imageStyle
            });
        },

        handleAction() {
            const that = this as any;
            that.triggerEvent("action", {
                scene: that.data.scene,
                action: that.data.resolvedAction
            });
        }
    }
});
