ai-chatbot/lib/agents/psd-text-color-config.ts
Your Name ab0dc8bce3
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
feat: add brand import and brand-aware psd settings
2026-04-02 14:42:18 +08:00

91 lines
2.6 KiB
TypeScript

import type { AgentPsdTextColorBrandConfig, AgentPsdTextColorConfig } from "@/types/agent";
import { normalizeHexColor } from "@/lib/agents/psd-text-layout";
export const AGENT_PSD_TEXT_COLOR_CONFIG_KEY = "agent_psd_text_color_config";
export const defaultAgentPsdTextColorConfig: AgentPsdTextColorConfig = {
enabled: false,
colors: ["#ffffff", "#111111"],
brandConfigs: [],
};
function normalizeBrandName(value: unknown) {
return String(value || "").trim().slice(0, 64);
}
function normalizeColors(value: unknown, fallback: [string, string]) {
const colors = Array.isArray(value)
? value
.map((item) => normalizeHexColor(String(item || ""), ""))
.filter(Boolean)
.slice(0, 2)
: [];
return [
colors[0] || fallback[0],
colors[1] || colors[0] || fallback[1],
] as [string, string];
}
export function normalizeAgentPsdTextColorConfig(
value: unknown
): AgentPsdTextColorConfig {
const raw =
value && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: {};
const baseColors = normalizeColors(raw.colors, defaultAgentPsdTextColorConfig.colors);
const uniqueBrandConfigs = new Map<string, AgentPsdTextColorBrandConfig>();
if (Array.isArray(raw.brandConfigs)) {
raw.brandConfigs.forEach((item) => {
const record =
item && typeof item === "object" && !Array.isArray(item)
? (item as Record<string, unknown>)
: {};
const brandName = normalizeBrandName(record.brandName);
if (!brandName) {
return;
}
uniqueBrandConfigs.set(brandName.toLowerCase(), {
brandName,
enabled: record.enabled === true,
colors: normalizeColors(record.colors, baseColors),
});
});
}
return {
enabled: raw.enabled === true,
colors: baseColors,
brandConfigs: Array.from(uniqueBrandConfigs.values()).sort((left, right) =>
left.brandName.localeCompare(right.brandName, "zh-CN")
),
};
}
export function resolveAgentPsdTextColorConfigForBrand(
config: AgentPsdTextColorConfig | null | undefined,
brandName?: string | null
): AgentPsdTextColorConfig {
const normalized = normalizeAgentPsdTextColorConfig(config);
const requestedBrand = normalizeBrandName(brandName);
if (!requestedBrand) {
return normalized;
}
const matchedBrandConfig = (normalized.brandConfigs || []).find(
(entry) => entry.brandName.toLowerCase() === requestedBrand.toLowerCase()
);
if (!matchedBrandConfig) {
return normalized;
}
return {
...normalized,
enabled: matchedBrandConfig.enabled,
colors: matchedBrandConfig.colors,
};
}