import { CommonDefine } from '../core/Define';

/** 地砖：仅 Baseblock / JumpBlock */
const GROUND_TILES = new Set<string>([CommonDefine.BlockBase, CommonDefine.BlockJump]);

/** 墙砖与墙饰（Border 层） */
const BORDER_TILES = new Set<string>([
    CommonDefine.BlockWall,
    'kuai11',
    'Decor23',
    '素材切图-23',
    '素材切图2-23',
    '小游戏素材红色_03',
]);

export function isGroundTileName(tileName: string): boolean {
    return GROUND_TILES.has(tileName);
}

export function isWallBlockTileName(tileName: string): boolean {
    return tileName === CommonDefine.BlockWall;
}

/** Ground 层：强制地砖属性，拒绝墙砖名 */
export function normalizeGroundTile(value: unknown): string {
    return value === CommonDefine.BlockJump ? CommonDefine.BlockJump : CommonDefine.BlockBase;
}

/** Border 层：强制墙砖属性，拒绝地砖名 */
export function normalizeBorderTile(value: unknown): string {
    if (value === true || value == null) return CommonDefine.BlockWall;
    if (typeof value !== 'string') return CommonDefine.BlockWall;
    const v = value.trim();
    if (!v || GROUND_TILES.has(v)) return CommonDefine.BlockWall;
    if (v === CommonDefine.BlockWall || BORDER_TILES.has(v)) return v;
    return v;
}

/** 按层级解析贴图 key（层级优先于裸值） */
export function resolveTileNameForLayer(layer: 'ground' | 'border', value: unknown): string {
    return layer === 'ground' ? normalizeGroundTile(value) : normalizeBorderTile(value);
}

export function resolveTileNameAtCell(
    layer: 'ground' | 'border',
    key: string,
    ground?: Record<string, string>,
    border?: Record<string, boolean | string>,
): string {
    if (layer === 'ground') {
        return normalizeGroundTile(ground?.[key]);
    }
    return normalizeBorderTile(border?.[key]);
}
