import * as A from "fp-ts-esm/Array";
import { identity, pipe } from "fp-ts-esm/function";
import { concatAll } from "fp-ts-esm/Monoid";
import * as R from "fp-ts-esm/Record";
import { first } from "fp-ts-esm/Semigroup";
import * as T from "fp-ts-esm/Tuple";
import { firstRight } from "../../../fp-utils.js";
import * as SRT from "../struct-refined-types.js";
const mergeJsonMap = R.getMonoid(first());
const toRecord = ([k, v]) => ({ [k]: v });
const convertStructToJson = (message) => pipe(
  message.fields,
  Object.entries,
  A.map(T.bimap(convertValueToJson, identity)),
  A.map(toRecord),
  concatAll(mergeJsonMap)
);
const convertValueToJson = (message) => firstRight(message, null, [
  [SRT.isBoolValue, ({ boolValue }) => boolValue],
  [SRT.isNumberValue, ({ numberValue }) => numberValue],
  [SRT.isStringValue, ({ stringValue }) => stringValue],
  [SRT.isNullValue, () => null],
  [SRT.isArrayValue, (m) => convertListToJson(m.listValue || { values: [] })],
  [SRT.isStructValue, (m) => convertStructToJson(m.structValue || { fields: {} })]
]);
const convertListToJson = (message) => pipe(message.values, A.map(convertValueToJson));
export {
  convertStructToJson
};
//# sourceMappingURL=convert-struct-to-json.js.map
