'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var renderCodeBlockToHtml = require('../renderers/render-code-block-to-html.js');
var renderCodeInlineToHtml = require('../renderers/render-code-inline-to-html.js');

/**
 * 恢复代码块为渲染后的HTML，将占位符替换为HTML内容
 * 这个函数负责将processor中的占位符恢复为完整的HTML渲染结果
 */
function restoreCodeBlocksAsHtml(text, codeBlockPlaceholders, inlineCodePlaceholders) {
  let restoredText = text;

  // 恢复代码块占位符为HTML
  codeBlockPlaceholders.forEach((originalCode, index) => {
    const placeholder = `@@CODE_BLOCK_PLACEHOLDER_${index}@@`;
    const renderedHtml = renderCodeBlockToHtml.renderCodeBlockToHtml(originalCode);
    restoredText = restoredText.replace(placeholder, renderedHtml);
  });

  // 恢复行内代码占位符为HTML
  inlineCodePlaceholders.forEach((originalCode, index) => {
    const placeholder = `@@INLINE_CODE_PLACEHOLDER_${index}@@`;
    const renderedHtml = renderCodeInlineToHtml.renderCodeInlineToHtml(originalCode);
    restoredText = restoredText.replace(placeholder, renderedHtml);
  });
  return restoredText;
}

/**
 * 代码后处理钩子 - 将占位符恢复为渲染后的HTML
 */
function codePostprocess(content, context) {
  const state = context.state;

  // 从插件状态中获取占位符数组
  const codeBlockPlaceholders = state.codePlaceholders || [];
  const inlineCodePlaceholders = state.inlineCodePlaceholders || [];
  return restoreCodeBlocksAsHtml(content, codeBlockPlaceholders, inlineCodePlaceholders);
}

exports.codePostprocess = codePostprocess;
exports.restoreCodeBlocksAsHtml = restoreCodeBlocksAsHtml;
