import * as mui from "@mui/material"
import { palettes } from "../options/palette"
let validColors: string[] | undefined
/**
* @__NO_SIDE_EFFECTS__
*/
export const isColorValid = /* @__PURE__ */ (color?: unknown) => {
if (process.env.NODE_ENV === `production`) return
if (typeof color !== `string`) return
if (!validColors) {
const tones = Object.keys({
main: true,
light: true,
dark: true,
contrastText: true,
} satisfies Record<keyof mui.SimplePaletteColorOptions, true>)
const colors = Object.keys({
primary: true,
secondary: true,
error: true,
warning: true,
info: true,
success: true,
} satisfies Record<ColorWithTones, true>)
const text = Object.keys({
disabled: true,
primary: true,
secondary: true,
} satisfies Record<keyof mui.TypeText, true>)
const background = Object.keys({
default: true,
paper: true,
ground: true,
} satisfies Record<keyof mui.TypeBackground, true>)
/**
* Sometimes, we want to let the user to a color that is not in the palette (theme)
*/
const validStaticColors = [`white`]
/**
* A user can use a literal color, by using "mui.useTheme" and then pass a literal color
*/
const literalThemeColors = Object.keys(palettes).flatMap((paletteName) => {
const palette = palettes[paletteName]
const literals = new Set<string>() // to avoid duplicates
for (const key of Object.keys(palette)) {
const value = palette[key]
if (typeof value === `string`) {
literals.add(value)
continue
}
for (const valueKey of Object.keys(value)) {
const nestedValue = value[valueKey]
if (typeof nestedValue === `string`) {
literals.add(nestedValue)
continue
}
}
}
return [...literals]
})
validColors = [
...validStaticColors,
...literalThemeColors,
`primary`,
`secondary`,
...background.map((tone) => `background.${tone}`),
...text.map((tone) => `text.${tone}`),
...colors.flatMap((color) => tones.map((tone) => `${color}.${tone}`)),
]
}
if (!validColors.includes(color)) {
throw new Error(
`Invalid color: "${color}"\n` +
`Valid colors are: ${validColors.join(`, `)}`,
)
}
}