import Buffer from "buffer";
import processBrowser from "process/browser";
import console from "console-browserify";
var buffer = {
  Buffer
};
const addBase64ImagePrefix = (base64) => {
  if (!base64)
    return "";
  return base64.startsWith("data:") ? base64 : `data:image/png;base64,${base64}`;
};
function transformComputerUseDataToDump(data) {
  return {
    groupName: data.instruction,
    groupDescription: "",
    executions: [
      {
        sdkVersion: data.version,
        model_name: data.modelName,
        logTime: data.logTime,
        name: data.instruction,
        tasks: data.conversations.map((conv) => {
          var _a;
          if (conv.from === "human" && conv.value === "<image>") {
            return {
              status: "finished",
              type: "screenshot",
              timing: conv.timing,
              pageContext: {
                screenshotBase64: addBase64ImagePrefix(
                  conv.screenshotBase64 || ""
                ),
                screenshotBase64WithElementMarker: addBase64ImagePrefix(
                  conv.screenshotBase64WithElementMarker || ""
                ),
                size: ((_a = conv.screenshotContext) == null ? void 0 : _a.size) || { width: 0, height: 0 }
              },
              recorder: [
                {
                  type: "screenshot",
                  ts: conv.timing.start,
                  screenshot: addBase64ImagePrefix(
                    conv.screenshotBase64 || ""
                  ),
                  timing: "before screenshot"
                }
              ]
            };
          }
          if (conv.from === "gpt") {
            return {
              status: "finished",
              type: "Action",
              timing: conv.timing,
              recorder: [
                ...conv.screenshotBase64 ? [
                  {
                    type: "screenshot",
                    ts: conv.timing.start,
                    screenshot: addBase64ImagePrefix(
                      conv.screenshotBase64 || ""
                    ),
                    timing: "before screenshot"
                  }
                ] : [],
                ...conv.screenshotBase64WithElementMarker ? [
                  {
                    type: "screenshot",
                    ts: conv.timing.start,
                    screenshot: addBase64ImagePrefix(
                      conv.screenshotBase64WithElementMarker || ""
                    ),
                    timing: "after screenshot"
                  }
                ] : []
              ]
            };
          }
          return null;
        }).filter(
          (task) => task !== null
        )
      }
    ],
    attributes: {}
  };
}
export {
  transformComputerUseDataToDump
};
