'use strict';

require('fs');
require('path');
require('child_process');
var util = require('util');

// src/platform/core/helpers.ts
function stringifyUnknown(value) {
  if (value === void 0 || value === null) {
    return "";
  }
  if (typeof value === "string") {
    return value;
  }
  if (Buffer.isBuffer(value)) {
    return value.toString("utf8");
  }
  if (value instanceof Error) {
    return value.stack || value.message;
  }
  return util.inspect(value, { depth: 6, breakLength: 120 });
}
function getErrorSummary(error) {
  if (error instanceof Error) {
    return `${error.name}: ${error.message}`;
  }
  return stringifyUnknown(error) || "Unknown error";
}
function normalizeGenHtmlComponentName(componentName) {
  return componentName.trim();
}
function normalizeGenHtmlReferenceInput(reference) {
  return {
    description: reference.description.trim(),
    figmaAddress: reference.figmaAddress.trim()
  };
}
function normalizeGenHtmlComponentInput(component) {
  return {
    componentName: normalizeGenHtmlComponentName(component.componentName),
    references: component.references.map(normalizeGenHtmlReferenceInput).filter((reference) => Boolean(reference.figmaAddress))
  };
}

// src/features/gen-html-multiple/infrastructure/server-utils.ts
function createBatchStatusLogger(appendLog) {
  return {
    fail(message) {
      appendLog(`[fail] ${message}
`);
    },
    step(message) {
      appendLog(`[step] ${message}
`);
    },
    async track(message, task) {
      appendLog(`[track] ${message}
`);
      try {
        const result = await task();
        appendLog(`[done] ${message}
`);
        return result;
      } catch (error) {
        appendLog(`[error] ${message}
`);
        throw error;
      }
    }
  };
}
function respondJson(response, statusCode, payload) {
  response.statusCode = statusCode;
  response.setHeader("content-type", "application/json; charset=utf-8");
  response.end(`${JSON.stringify(payload)}
`);
}
function readJsonBody(request, trace) {
  return new Promise((resolve, reject) => {
    let raw = "";
    let settled = false;
    const rejectOnce = (step, error) => {
      if (settled) {
        return;
      }
      settled = true;
      trace("gen-html-multiple:request:body:error", `${step} ${getErrorSummary(error)}`);
      reject(error);
    };
    request.setEncoding("utf8");
    request.on("data", (chunk) => {
      if (settled) {
        return;
      }
      raw += chunk;
      if (raw.length > 1024 * 1024) {
        rejectOnce("body-too-large", new Error("\uC694\uCCAD \uBCF8\uBB38\uC774 \uB108\uBB34 \uD07D\uB2C8\uB2E4."));
        request.destroy();
      }
    });
    request.on("end", () => {
      if (settled) {
        return;
      }
      settled = true;
      if (!raw.trim()) {
        resolve({});
        return;
      }
      try {
        resolve(JSON.parse(raw));
      } catch (error) {
        reject(error instanceof Error ? error : new Error(String(error)));
      }
    });
    request.on("error", (error) => rejectOnce("request-error", error instanceof Error ? error : new Error(String(error))));
  });
}
function normalizeRequestComponents(value) {
  const rawComponents = Array.isArray(value) ? value : [];
  return rawComponents.map((entry) => {
    const component = entry;
    return normalizeGenHtmlComponentInput({
      componentName: String(component.componentName || ""),
      references: Array.isArray(component.references) ? component.references.map((reference) => {
        const currentReference = reference;
        return {
          description: String(currentReference.description || ""),
          figmaAddress: String(currentReference.figmaAddress || "")
        };
      }) : []
    });
  }).filter((component) => component.componentName && component.references.length > 0);
}

exports.createBatchStatusLogger = createBatchStatusLogger;
exports.normalizeRequestComponents = normalizeRequestComponents;
exports.readJsonBody = readJsonBody;
exports.respondJson = respondJson;
