"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var write_exports = {};
__export(write_exports, {
  writeIpcStub: () => writeIpcStub
});
module.exports = __toCommonJS(write_exports);
var import_node_process = __toESM(require("node:process"));
var import_safe = require("../fs/safe");
var import_parse = require("../schema/parse");
var import_date = require("../primordials/date");
var import_json = require("../primordials/json");
var import_internal = require("./_internal");
var import_directory = require("./directory");
var import_paths = require("./paths");
async function writeIpcStub(appName, data) {
  const stubPath = (0, import_paths.getIpcStubPath)(appName);
  await (0, import_directory.ensureIpcDirectory)(stubPath);
  const ipcData = {
    data,
    pid: import_node_process.default.pid,
    timestamp: (0, import_date.DateNow)()
  };
  const validated = (0, import_parse.parseSchema)(import_internal.IpcStubSchema, ipcData);
  const fs = (0, import_internal.getFs)();
  const flags = fs.constants.O_CREAT | fs.constants.O_WRONLY | fs.constants.O_EXCL | fs.constants.O_NOFOLLOW;
  let handle;
  try {
    handle = await fs.promises.open(stubPath, flags, 384);
  } catch (e) {
    const err = e;
    if (err.code === "EEXIST") {
      await (0, import_safe.safeDelete)(stubPath);
      handle = await fs.promises.open(stubPath, flags, 384);
    } else {
      throw err;
    }
  }
  try {
    await handle.writeFile((0, import_json.JSONStringify)(validated, void 0, 2), "utf8");
  } finally {
    await handle.close();
  }
  return stubPath;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  writeIpcStub
});
