import { TRANSPARENT_HEX_COLOR } from "./../../_shared/colors.js";
import { tryParseJson } from "./shared.js";
const isLayoutFillObject = (value) => typeof value === "object" && value !== null;
const isLayoutFillGradient = (value) => isLayoutFillObject(value);
const isNormalizedLayoutFill = (value) => value?.type === "COLOR" ? typeof value.color === "string" && !!value.color : value?.type === "GRADIENT" ? isLayoutFillGradient(value.gradient) : false;
const parseLayoutFill = (value) => {
  if (!isLayoutFillObject(value)) {
    return void 0;
  }
  if (typeof value.color === "string" && value.color) {
    return { type: "COLOR", color: value.color };
  }
  if (isLayoutFillGradient(value.gradient)) {
    return { type: "GRADIENT", gradient: value.gradient };
  }
  return void 0;
};
const toLayoutFill = (value) => {
  if (value?.gradient) {
    return { type: "GRADIENT", gradient: value.gradient };
  }
  if (value?.color) {
    return { type: "COLOR", color: value.color };
  }
  return void 0;
};
const getLegacyLayoutFillColorKey = (fillKey) => fillKey === "background" ? "backgroundColor" : "backdropColor";
function resolveLayoutFill(fill, legacyColor) {
  if (isNormalizedLayoutFill(fill)) {
    return fill;
  }
  const gradient = getLayoutFillGradient(fill);
  if (gradient) {
    return { type: "GRADIENT", gradient };
  }
  const color = getLayoutFillColor(fill);
  if (color !== void 0) {
    return { type: "COLOR", color };
  }
  if (typeof legacyColor === "string") {
    return { type: "COLOR", color: legacyColor };
  }
  return void 0;
}
const getLayoutFillColor = (fill) => fill && "color" in fill && typeof fill.color === "string" ? fill.color || void 0 : void 0;
const getLayoutFillGradient = (fill) => fill && "gradient" in fill && isLayoutFillGradient(fill.gradient) ? fill.gradient : void 0;
const getResolvedLayoutFillColor = (fill, legacyColor) => getLayoutFillColor(resolveLayoutFill(fill, legacyColor));
const getResolvedLayoutFillGradient = (fill, legacyColor) => getLayoutFillGradient(resolveLayoutFill(fill, legacyColor));
const hasVisibleLayoutFill = (fill) => {
  if (getLayoutFillGradient(fill)) {
    return true;
  }
  const color = getLayoutFillColor(fill);
  return !!color && color !== TRANSPARENT_HEX_COLOR;
};
const isTransparentOrEmptyLayoutFill = (fill) => !fill || getLayoutFillColor(fill) === TRANSPARENT_HEX_COLOR;
const clearTransparentLayoutFill = (fill) => {
  if (!fill || isTransparentOrEmptyLayoutFill(fill)) {
    return void 0;
  }
  return fill;
};
const getLayoutFillPatch = (fillKey, fill) => ({
  [fillKey]: fill,
  [getLegacyLayoutFillColorKey(fillKey)]: void 0
});
const parseLayoutFillHtmlAttr = (serializedFill, legacyColor, defaultColor) => {
  const fill = parseLayoutFill(tryParseJson(serializedFill));
  return {
    fill,
    legacyColor: getLayoutFillColor(fill) ?? (fill?.type === "GRADIENT" ? defaultColor ?? void 0 : legacyColor ?? defaultColor ?? void 0)
  };
};
const getLayoutFillHtmlAttrs = ({
  fill,
  legacyColor,
  fillKey
}) => {
  const serializedFillKey = `data-layout-${fillKey}`;
  const serializedColorKey = `data-layout-${fillKey}-color`;
  const resolvedLegacyColor = getLayoutFillColor(fill) ?? (fill ? void 0 : legacyColor);
  return {
    ...fill ? { [serializedFillKey]: JSON.stringify(fill) } : {},
    ...resolvedLegacyColor !== void 0 ? { [serializedColorKey]: resolvedLegacyColor } : {}
  };
};
export {
  clearTransparentLayoutFill,
  getLayoutFillColor,
  getLayoutFillGradient,
  getLayoutFillHtmlAttrs,
  getLayoutFillPatch,
  getLegacyLayoutFillColorKey,
  getResolvedLayoutFillColor,
  getResolvedLayoutFillGradient,
  hasVisibleLayoutFill,
  isTransparentOrEmptyLayoutFill,
  parseLayoutFillHtmlAttr,
  resolveLayoutFill,
  toLayoutFill
};
//# sourceMappingURL=layout-fill.js.map
