/**
 * テキスト設定用のヘルパー関数集
 */

/**
 * 編集画面でのフォントサイズ表示用のロジック
 * @param {Object} attributes - ブロック属性
 * @param {string} type - テキストタイプ（caption, title, description）
 * @param {string} device - デバイス（pc, sp）
 * @returns {string|undefined} フォントサイズ値
 */
export const getDirectFontSize = (attributes, type, device = "pc") => {
  if (!attributes) return undefined;

  const fontSize = attributes[`${type}FontSize`];
  const customSizePC = attributes[`custom${type.charAt(0).toUpperCase() + type.slice(1)}FontSizePC`];
  const customSizeSP = attributes[`custom${type.charAt(0).toUpperCase() + type.slice(1)}FontSizeSP`];
  const fontSizeUnitPC = attributes[`${type}FontSizeUnitPC`] || "px";
  const fontSizeUnitSP = attributes[`${type}FontSizeUnitSP`] || "vw";

  const safeValue = (value, fallback = "") => {
    if (value === undefined || value === null) return fallback;
    return value;
  };

  const result = (() => {
    if (device === "pc") {
      if (fontSize === "custom") {
        const customSize = safeValue(customSizePC);
        if (customSize) {
          return `${customSize}${fontSizeUnitPC}`;
        }
      } else if (fontSize && typeof fontSize === "number") {
        return `${fontSize}${fontSizeUnitPC}`;
      }
    } else if (device === "sp") {
      if (fontSize === "custom") {
        const customSize = safeValue(customSizeSP);
        return customSize !== "" ? `${customSize}${fontSizeUnitSP}` : "";
      } else if (fontSize && typeof fontSize === "number") {
        return `${fontSize}${fontSizeUnitSP}`;
      }
    }
    return undefined;
  })();

  return result;
};

/**
 * フォントサイズの値を取得するヘルパー関数
 * @param {Object} attributes - ブロック属性
 * @param {string} type - テキストタイプ（caption, title, description）
 * @param {string} device - デバイス（pc, sp）
 * @returns {string} フォントサイズ値
 */
export const getFontSizeValue = (attributes, type, device = "pc") => {
  if (!attributes) return "";

  const fontSize = attributes[`${type}FontSize`];
  const customSizePC = attributes[`custom${type.charAt(0).toUpperCase() + type.slice(1)}FontSizePC`];
  const customSizeSP = attributes[`custom${type.charAt(0).toUpperCase() + type.slice(1)}FontSizeSP`];
  const fontSizeUnitPC = attributes[`${type}FontSizeUnitPC`] || "px";
  const fontSizeUnitSP = attributes[`${type}FontSizeUnitSP`] || "vw";

  const safeValue = (value, fallback = "") => {
    if (value === undefined || value === null) return fallback;
    return value;
  };

  const result = (() => {
    if (device === "pc") {
      if (fontSize === "custom") {
        const customSize = safeValue(customSizePC);
        if (customSize) {
          return `${customSize}${fontSizeUnitPC}`;
        }
      } else if (fontSize && typeof fontSize === "number") {
        return `${fontSize}${fontSizeUnitPC}`;
      }
    } else if (device === "sp") {
      if (fontSize === "custom") {
        const customSize = safeValue(customSizeSP);
        if (customSize !== "") {
          return `${customSize}${fontSizeUnitSP}`;
        } else {
          return "";
        }
      } else if (fontSize && typeof fontSize === "number") {
        return `${fontSize}${fontSizeUnitSP}`;
      }
    }
    return "inherit";
  })();

  return result;
};

/**
 * 間隔の値を取得するヘルパー関数
 * @param {Object} attributes - ブロック属性
 * @param {string} type - 間隔タイプ（captionToTitle, titleToDescription, description）
 * @param {string} device - デバイス（pc, sp）
 * @returns {string} 間隔値
 */
export const getSpacingValue = (attributes, type, device = "pc") => {
  if (!attributes) return "";

  const isPC = device === "pc";
  const unitSuffix = isPC ? "PC" : "SP";
  const spacingUnit = attributes[`spacingUnit${unitSuffix}`] || (isPC ? "px" : "vw");

  let spacingAttr;
  if (type === "captionToTitle") {
    spacingAttr = `captionToTitleSpacing${unitSuffix}`;
  } else if (type === "titleToDescription") {
    spacingAttr = `titleToDescriptionSpacing${unitSuffix}`;
  } else if (type === "description") {
    spacingAttr = `descriptionMargin${unitSuffix}`;
  }

  const spacingValue = attributes[spacingAttr];

  if (spacingValue && spacingValue !== "") {
    return `${spacingValue}${spacingUnit}`;
  }

  return "0px";
};

/**
 * 行間の値を取得するヘルパー関数
 * @param {Object} attributes - ブロック属性
 * @param {string} type - テキストタイプ（caption, title, description）
 * @param {string} device - デバイス（pc, sp）
 * @returns {string} 行間値
 */
export const getLineHeightValue = (attributes, type, device = "pc") => {
  if (!attributes) return "";

  const lineHeightAttr = `${type}LineHeight${device.toUpperCase()}`;
  const unitAttr = `lineHeightUnit${device.toUpperCase()}`;
  const lineHeightValue = attributes[lineHeightAttr];
  const unit = attributes[unitAttr] || "";

  if (lineHeightValue && lineHeightValue !== "") {
    return `${lineHeightValue}${unit}`;
  }

  return "inherit";
};

/**
 * フォントファミリーの値を取得するヘルパー関数
 * @param {Object} attributes - ブロック属性
 * @param {string} type - テキストタイプ（caption, title, description）
 * @returns {string} フォントファミリー値
 */
export const getFontFamilyValue = (attributes, type) => {
  if (!attributes) return "";

  const fontFamilySlug = attributes[`${type}FontFamily`] || "noto-sans-jp";

  // テーマのtheme.jsonに定義されたフォントファミリーのCSS変数を返す
  return `var(--wp--preset--font-family--${fontFamilySlug})`;
};

/**
 * テキストシャドウの値を取得するヘルパー関数
 * @param {Object} attributes - ブロック属性
 * @param {string} type - テキストタイプ（caption, title, description）
 * @returns {string} テキストシャドウ値
 */
export const getTextShadowValue = (attributes, type) => {
  if (!attributes) return "none";

  const enabledAttr = `${type}TextShadowEnabled`;
  const offsetXAttr = `${type}TextShadowOffsetX`;
  const offsetYAttr = `${type}TextShadowOffsetY`;
  const blurAttr = `${type}TextShadowBlur`;
  const colorAttr = `${type}TextShadowColor`;

  const isEnabled = attributes[enabledAttr];

  if (!isEnabled) {
    return "none";
  }

  const offsetX = attributes[offsetXAttr] || "0";
  const offsetY = attributes[offsetYAttr] || "0";
  const blur = attributes[blurAttr] || "1";
  const color = attributes[colorAttr] || "#000000";

  return `${offsetX}px ${offsetY}px ${blur}px ${color}`;
};

/**
 * テキスト設定のCSS変数を生成する
 * @param {Object} attributes - ブロック属性
 * @returns {Object} CSS変数オブジェクト
 */
export const getTextBasicCSSVariables = (attributes) => {
  return {
    "--caption-font-size-pc": getFontSizeValue(attributes, "caption", "pc"),
    "--caption-font-size-sp": getFontSizeValue(attributes, "caption", "sp"),
    "--title-font-size-pc": getFontSizeValue(attributes, "title", "pc"),
    "--title-font-size-sp": getFontSizeValue(attributes, "title", "sp"),
    "--description-font-size-pc": getFontSizeValue(attributes, "description", "pc"),
    "--description-font-size-sp": getFontSizeValue(attributes, "description", "sp"),
    "--caption-to-title-spacing-pc": getSpacingValue(attributes, "captionToTitle", "pc"),
    "--caption-to-title-spacing-sp": getSpacingValue(attributes, "captionToTitle", "sp"),
    "--title-to-description-spacing-pc": getSpacingValue(attributes, "titleToDescription", "pc"),
    "--title-to-description-spacing-sp": getSpacingValue(attributes, "titleToDescription", "sp"),
    "--caption-line-height-pc": getLineHeightValue(attributes, "caption", "pc"),
    "--caption-line-height-sp": getLineHeightValue(attributes, "caption", "sp"),
    "--title-line-height-pc": getLineHeightValue(attributes, "title", "pc"),
    "--title-line-height-sp": getLineHeightValue(attributes, "title", "sp"),
    "--description-line-height-pc": getLineHeightValue(attributes, "description", "pc"),
    "--description-line-height-sp": getLineHeightValue(attributes, "description", "sp"),
    "--description-margin-pc": getSpacingValue(attributes, "description", "pc"),
    "--description-margin-sp": getSpacingValue(attributes, "description", "sp"),
    "--caption-color": attributes.captionColor || "#666666",
    "--title-color": attributes.titleColor || "#2563eb",
    "--description-color": attributes.descriptionColor || "#666666",
    "--caption-font-family": getFontFamilyValue(attributes, "caption"),
    "--title-font-family": getFontFamilyValue(attributes, "title"),
    "--description-font-family": getFontFamilyValue(attributes, "description"),
    "--caption-text-shadow": getTextShadowValue(attributes, "caption"),
    "--title-text-shadow": getTextShadowValue(attributes, "title"),
    "--description-text-shadow": getTextShadowValue(attributes, "description"),
  };
};

/**
 * デフォルト属性値を取得する
 * @returns {Object} デフォルト属性オブジェクト
 */
export const getTextBasicDefaultAttributes = () => {
  return {
    captionFontSize: "custom",
    titleFontSize: "custom",
    descriptionFontSize: "custom",
    customCaptionFontSizePC: "14",
    customCaptionFontSizeSP: "3.5",
    customTitleFontSizePC: "48",
    customTitleFontSizeSP: "12",
    customDescriptionFontSizePC: "16",
    customDescriptionFontSizeSP: "4",
    captionFontSizeUnitPC: "px",
    captionFontSizeUnitSP: "vw",
    titleFontSizeUnitPC: "px",
    titleFontSizeUnitSP: "vw",
    descriptionFontSizeUnitPC: "px",
    descriptionFontSizeUnitSP: "vw",
    captionToTitleSpacingPC: "16",
    captionToTitleSpacingSP: "4",
    titleToDescriptionSpacingPC: "16",
    titleToDescriptionSpacingSP: "4",
    spacingUnitPC: "px",
    spacingUnitSP: "vw",
    captionLineHeightPC: "1.5",
    captionLineHeightSP: "1.5",
    titleLineHeightPC: "1.2",
    titleLineHeightSP: "1.2",
    descriptionLineHeightPC: "1.6",
    descriptionLineHeightSP: "1.6",
    lineHeightUnitPC: "",
    lineHeightUnitSP: "",
    descriptionMarginPC: "0",
    descriptionMarginSP: "0",
    captionColor: "#666666",
    titleColor: "#2563eb",
    descriptionColor: "#666666",
    captionFontFamily: "noto-sans-jp",
    titleFontFamily: "noto-sans-jp",
    descriptionFontFamily: "noto-sans-jp",
    captionTextShadowEnabled: false,
    captionTextShadowOffsetX: "0",
    captionTextShadowOffsetY: "0",
    captionTextShadowBlur: "1",
    captionTextShadowColor: "#000000",
    titleTextShadowEnabled: false,
    titleTextShadowOffsetX: "0",
    titleTextShadowOffsetY: "0",
    titleTextShadowBlur: "1",
    titleTextShadowColor: "#000000",
    descriptionTextShadowEnabled: false,
    descriptionTextShadowOffsetX: "0",
    descriptionTextShadowOffsetY: "0",
    descriptionTextShadowBlur: "1",
    descriptionTextShadowColor: "#000000",
  };
};
