import { expect } from "@playwright/test";
import { apiRequest } from "./api.js";
import { readJsonSafe } from "./generalFixtures.js";
import { readJsonSafe as readJsonSafe2 } from "./generalFixtures.js";
function isRecord(value) {
  return typeof value === "object" && value !== null;
}
function findStringByKeys(value, keys) {
  if (!isRecord(value)) return null;
  for (const key of keys) {
    const candidate = value[key];
    if (typeof candidate === "string" && candidate.trim().length > 0) {
      return candidate.trim();
    }
  }
  for (const nested of Object.values(value)) {
    if (Array.isArray(nested)) continue;
    const found = findStringByKeys(nested, keys);
    if (found) return found;
  }
  return null;
}
async function createEntity(request, token, path, data, idKeys) {
  const response = await apiRequest(request, "POST", path, { token, data });
  const payload = await readJsonSafe(response);
  expect(response.ok(), `Failed POST ${path}: ${response.status()}`).toBeTruthy();
  const id = findStringByKeys(payload, idKeys);
  expect(id, `No id in ${path} response`).toBeTruthy();
  return id;
}
async function createCompanyFixture(request, token, displayName) {
  return createEntity(request, token, "/api/customers/companies", { displayName }, ["id", "entityId", "companyId"]);
}
async function createPersonFixture(request, token, input) {
  const data = {
    firstName: input.firstName,
    lastName: input.lastName,
    displayName: input.displayName
  };
  if (input.companyEntityId) {
    data.companyEntityId = input.companyEntityId;
  }
  return createEntity(request, token, "/api/customers/people", data, ["id", "entityId", "personId"]);
}
async function createDealFixture(request, token, input) {
  const data = { title: input.title };
  if (input.companyIds?.length) data.companyIds = input.companyIds;
  if (input.personIds?.length) data.personIds = input.personIds;
  if (input.pipelineId) data.pipelineId = input.pipelineId;
  if (input.pipelineStageId) data.pipelineStageId = input.pipelineStageId;
  if (input.valueAmount !== void 0) data.valueAmount = input.valueAmount;
  if (input.valueCurrency) data.valueCurrency = input.valueCurrency;
  if (input.status) data.status = input.status;
  if (input.expectedCloseAt) data.expectedCloseAt = input.expectedCloseAt;
  if (input.ownerUserId) data.ownerUserId = input.ownerUserId;
  if (input.closureOutcome) data.closureOutcome = input.closureOutcome;
  return createEntity(request, token, "/api/customers/deals", data, ["dealId", "id", "entityId"]);
}
async function createPipelineFixture(request, token, input) {
  const data = { name: input.name };
  if (input.isDefault !== void 0) data.isDefault = input.isDefault;
  return createEntity(request, token, "/api/customers/pipelines", data, ["id", "pipelineId"]);
}
async function createPipelineStageFixture(request, token, input) {
  const data = { pipelineId: input.pipelineId, label: input.label };
  if (input.order !== void 0) data.order = input.order;
  return createEntity(request, token, "/api/customers/pipeline-stages", data, ["id", "stageId"]);
}
async function deleteEntityByBody(request, token, path, id) {
  if (!token || !id) return;
  try {
    await apiRequest(request, "DELETE", path, { token, data: { id } });
  } catch {
    return;
  }
}
async function deleteEntityIfExists(request, token, path, id) {
  if (!token || !id) return;
  try {
    await apiRequest(request, "DELETE", `${path}?id=${encodeURIComponent(id)}`, { token });
  } catch {
    return;
  }
}
export {
  createCompanyFixture,
  createDealFixture,
  createPersonFixture,
  createPipelineFixture,
  createPipelineStageFixture,
  deleteEntityByBody,
  deleteEntityIfExists,
  readJsonSafe2 as readJsonSafe
};
//# sourceMappingURL=crmFixtures.js.map
