'use strict';

function buildGraphQlQuery(args) {
  const queryHeaderStart = "query (";
  const queryHeaderEnd = ")";
  const queryHeaderPaginationOrderByParams = `$paginationInfo: Pagination, $orderByInfo: ${args.type.slice(0, -1)}OrderBy`;
  const filterParams = args.filterCondition?.clauseVariable?.map((cl) => {
    return `$${cl.clauseVariableName}: ${cl.clauseVariableType}`;
  }).join(", ");
  const params = [queryHeaderPaginationOrderByParams, filterParams].filter(Boolean).join(", ");
  let query = `${queryHeaderStart}${params}${queryHeaderEnd}{${args.type}`;
  const whereClause = buildWhereClause(args.whereClause);
  const paginationClause = "pagination: $paginationInfo";
  const orderByClause = "orderBy: $orderByInfo";
  {
    query += " (";
    query += [whereClause, orderByClause, paginationClause].filter(Boolean).join(", ");
    query += ") ";
  }
  query += ` {${args.queryBody} } }`;
  return query.replace(/\s+/g, " ").trim();
}
function buildWhereClause(whereClause) {
  return whereClause ? `where: {${whereClause}}` : "";
}
function buildOrderByVariables(pagination) {
  const orderByVariable = {};
  if (pagination?.sortField !== void 0) {
    orderByVariable[pagination.sortField] = pagination.order?.toUpperCase() ?? "ASC";
  }
  return orderByVariable;
}
function buildPaginationVariables(pagination) {
  const paginationVariable = {};
  if (pagination?.limit !== void 0) {
    paginationVariable.limit = pagination.limit;
  }
  if (pagination?.offset !== void 0) {
    paginationVariable.offset = pagination.offset;
  }
  return paginationVariable;
}
function buildQueryParamVariable(pagination, filterCondition) {
  const paramVariables = {
    paginationInfo: buildPaginationVariables(pagination),
    orderByInfo: buildOrderByVariables(pagination)
  };
  filterCondition?.clauseVariable?.forEach((p) => {
    paramVariables[p.clauseVariableName] = p.formattedValue;
  });
  return paramVariables;
}

exports.buildGraphQlQuery = buildGraphQlQuery;
exports.buildOrderByVariables = buildOrderByVariables;
exports.buildPaginationVariables = buildPaginationVariables;
exports.buildQueryParamVariable = buildQueryParamVariable;
//# sourceMappingURL=queryBuilder.cjs.js.map
