// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Curry from "rescript/lib/es6/curry.js";
import * as Decco from "decco/src/Decco.bs.js";
import * as Js_exn from "rescript/lib/es6/js_exn.js";
import * as Js_dict from "rescript/lib/es6/js_dict.js";
import * as Js_json from "rescript/lib/es6/js_json.js";
import * as Belt_Int from "rescript/lib/es6/belt_Int.js";
import * as Js_string from "rescript/lib/es6/js_string.js";
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
import * as Belt_Float from "rescript/lib/es6/belt_Float.js";
import * as Belt_Option from "rescript/lib/es6/belt_Option.js";
import * as Belt_Result from "rescript/lib/es6/belt_Result.js";
import * as Belt_MapString from "rescript/lib/es6/belt_MapString.js";
import * as Belt_SetString from "rescript/lib/es6/belt_SetString.js";
import ParseISO from "date-fns/parseISO";
import FormatISO from "date-fns/formatISO";

function date_0(x) {
  return Js_string.substring(0, 10, FormatISO(x));
}

function date_1(x) {
  return Belt_Result.map(Decco.stringFromJson(x), (function (xx) {
                return ParseISO(Js_string.substring(0, 10, xx));
              }));
}

var date = [
  date_0,
  date_1
];

function floatOfString_0(x) {
  return String(x);
}

function floatOfString_1(x) {
  return Belt_Option.mapWithDefault(Belt_Option.flatMap(Js_json.decodeString(x), Belt_Float.fromString), Decco.error(undefined, "Unable to parse", x), (function (xx) {
                return {
                        TAG: /* Ok */0,
                        _0: xx
                      };
              }));
}

var floatOfString = [
  floatOfString_0,
  floatOfString_1
];

function intOfString_0(x) {
  return String(x);
}

function intOfString_1(x) {
  return Belt_Option.mapWithDefault(Belt_Option.flatMap(Js_json.decodeString(x), Belt_Int.fromString), Decco.error(undefined, "Unable to parse", x), (function (xx) {
                return {
                        TAG: /* Ok */0,
                        _0: xx
                      };
              }));
}

var intOfString = [
  intOfString_0,
  intOfString_1
];

function mapString_0(encode, map) {
  return Js_dict.fromArray(Belt_Array.map(Belt_MapString.toArray(map), (function (param) {
                    return [
                            param[0],
                            Curry._1(encode, param[1])
                          ];
                  })));
}

function mapString_1(decode, json) {
  var result = Decco.dictFromJson(decode, json);
  if (result.TAG === /* Ok */0) {
    return {
            TAG: /* Ok */0,
            _0: Belt_MapString.fromArray(Js_dict.entries(result._0))
          };
  } else {
    return {
            TAG: /* Error */1,
            _0: result._0
          };
  }
}

var mapString = [
  mapString_0,
  mapString_1
];

function setString_0(x) {
  return Belt_Array.map(Belt_SetString.toArray(x), Decco.stringToJson);
}

function setString_1(json) {
  var result = Decco.arrayFromJson(Decco.stringFromJson, json);
  if (result.TAG === /* Ok */0) {
    return {
            TAG: /* Ok */0,
            _0: Belt_SetString.fromArray(result._0)
          };
  } else {
    return {
            TAG: /* Error */1,
            _0: result._0
          };
  }
}

var setString = [
  setString_0,
  setString_1
];

var Codecs = {
  date: date,
  floatOfString: floatOfString,
  intOfString: intOfString,
  mapString: mapString,
  setString: setString
};

function noop(prim) {
  return prim;
}

function unwrapJson(decode, x) {
  return Belt_Option.mapWithDefault(Belt_Option.map(Js_json.decodeString(x), (function (prim) {
                    return JSON.parse(prim);
                  })), Decco.error(undefined, "Unable to parse", x), decode);
}

function errorToString(param) {
  return " Parsing error at " + param.path + ": " + param.message + "";
}

function getExn(result) {
  if (result.TAG === /* Ok */0) {
    return result._0;
  } else {
    return Js_exn.raiseError(errorToString(result._0));
  }
}

var mapString_encode = Curry.__2(mapString_0);

var mapString_decode = Curry.__2(mapString_1);

var setString_encode = Curry.__1(setString_0);

var setString_decode = Curry.__1(setString_1);

function decodeWithFallback(decode, fallback, json) {
  var x = Curry._1(decode, json);
  if (x.TAG === /* Ok */0) {
    return x;
  } else {
    return Curry._1(fallback, json);
  }
}

export {
  Codecs ,
  noop ,
  unwrapJson ,
  errorToString ,
  getExn ,
  mapString_encode ,
  mapString_decode ,
  setString_encode ,
  setString_decode ,
  decodeWithFallback ,
}
/* date-fns/parseISO Not a pure module */
