"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
var _exportNames = {
  useForm: true,
  useAtomicForm: true,
  useFormRev: true,
  useSubmittedFormData: true,
  useFormSafe: true,
  useAtomicFormSafe: true,
  useForms: true,
  useFormGroup: true,
  useFormGroups: true,
  useField: true,
  useFields: true,
  useFormData: true,
  useTags: true,
  useFieldUsageCounts: true,
  useFormgroupUsageCounts: true,
  useFormHandlers: true,
  useStandardizedForm: true,
  useMutations: true
};
exports.useMutations = useMutations;
exports.useStandardizedForm = exports.useFormHandlers = exports.useFormgroupUsageCounts = exports.useFieldUsageCounts = exports.useTags = exports.useFormData = exports.useFields = exports.useField = exports.useFormGroups = exports.useFormGroup = exports.useForms = exports.useAtomicFormSafe = exports.useFormSafe = exports.useSubmittedFormData = exports.useFormRev = exports.useAtomicForm = exports.useForm = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var client = _interopRequireWildcard(require("./client"));

Object.keys(client).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === client[key]) return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function get() {
      return client[key];
    }
  });
});

var _legacyMode = require("./legacyMode");

var _useNormalizeConfig = _interopRequireWildcard(require("./useNormalizeConfig"));

var _utils = require("./utils");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var useForm = function useForm(formId, options) {
  var _client$useForm;

  return _client$useForm = client.useForm({
    formId: formId
  }), (0, _useNormalizeConfig["default"])(options)(_client$useForm);
};

exports.useForm = useForm;

var useAtomicForm = function useAtomicForm(formId, formRev, layer, options) {
  var normalize = (0, _useNormalizeConfig["default"])(options);

  if (formRev) {
    return normalize(client.useFormRev({
      formId: formId,
      formRev: formRev,
      layer: layer
    }));
  } else {
    return normalize(client.useAtomicForm({
      formId: formId,
      layer: layer
    }));
  }
};

exports.useAtomicForm = useAtomicForm;

var useFormRev = function useFormRev(formId, formRev) {
  return client.useFormRev({
    formId: formId,
    formRev: formRev
  });
};

exports.useFormRev = useFormRev;

var useSubmittedFormData = function useSubmittedFormData(formId, formRev, credentials, dataId, dataIsDraft, layer) {
  if (formRev) {
    return client.useFormAndFormDataByRevision({
      formId: formId,
      formRev: formRev,
      dataId: dataId,
      credentials: credentials,
      dataIsDraft: dataIsDraft,
      layer: layer
    });
  } else {
    return client.useFormDataFromSubmissionHandler({
      formId: formId,
      dataId: dataId,
      credentials: credentials,
      dataIsDraft: dataIsDraft,
      layer: layer
    });
  }
};

exports.useSubmittedFormData = useSubmittedFormData;

var useFormSafe = function useFormSafe(formId, options) {
  var normalize = (0, _useNormalizeConfig["default"])(options);

  var _client$useFormSafe = client.useFormSafe({
    formId: formId
  }),
      _client$useFormSafe2 = (0, _slicedToArray2["default"])(_client$useFormSafe, 2),
      error = _client$useFormSafe2[0],
      data = _client$useFormSafe2[1];

  return [error, data ? normalize(data) : data];
};

exports.useFormSafe = useFormSafe;

var useAtomicFormSafe = function useAtomicFormSafe(formId, options) {
  var normalize = (0, _useNormalizeConfig["default"])(options);

  var _client$useAtomicForm = client.useAtomicFormSafe({
    formId: formId
  }),
      _client$useAtomicForm2 = (0, _slicedToArray2["default"])(_client$useAtomicForm, 2),
      error = _client$useAtomicForm2[0],
      data = _client$useAtomicForm2[1];

  return [error, data ? normalize(data) : data];
};

exports.useAtomicFormSafe = useAtomicFormSafe;

var useForms = function useForms() {
  var _$draft$published$pub;

  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      status = _ref.status,
      visibility = _ref.visibility;

  var options = arguments.length > 1 ? arguments[1] : undefined;
  return _$draft$published$pub = {
    '': client.useForms,
    draft: client.useDraftForms,
    published: client.usePublishedForms,
    'published/public': client.usePublishedPublicForms
  }[[status, visibility].filter(Boolean).join('/')](), (0, _useNormalizeConfig.useNormalizeConfigs)(options)(_$draft$published$pub);
};

exports.useForms = useForms;

var useFormGroup = function useFormGroup(formGroupId, options) {
  var _client$useFormGroup, _options;

  return _client$useFormGroup = client.useFormGroup({
    formGroupId: formGroupId
  }), (0, _useNormalizeConfig["default"])((_options = options, (0, _utils.setDefaultType)('formGroup')(_options)))(_client$useFormGroup);
};

exports.useFormGroup = useFormGroup;

var useFormGroups = function useFormGroups(options) {
  var _client$useFormGroups, _options2;

  return _client$useFormGroups = client.useFormGroups(), (0, _useNormalizeConfig.useNormalizeConfigs)((_options2 = options, (0, _utils.setDefaultType)('formGroup')(_options2)))(_client$useFormGroups);
};

exports.useFormGroups = useFormGroups;

var useField = function useField(fieldId, options) {
  var _client$useField;

  return _client$useField = client.useField({
    fieldId: fieldId
  }), (0, _useNormalizeConfig["default"])(options)(_client$useField);
};

exports.useField = useField;

var useFields = function useFields(options) {
  var _client$useFields;

  return _client$useFields = client.useFields(), (0, _useNormalizeConfig.useNormalizeConfigs)(options)(_client$useFields);
};

exports.useFields = useFields;

var useFormData = function useFormData(dataId) {
  return client.useFormData({
    dataId: dataId
  });
};

exports.useFormData = useFormData;

var useTags = function useTags() {
  return client.useFormTags();
};

exports.useTags = useTags;

var useFieldUsageCounts = function useFieldUsageCounts() {
  return client.useFieldUsageCount();
};

exports.useFieldUsageCounts = useFieldUsageCounts;

var useFormgroupUsageCounts = function useFormgroupUsageCounts() {
  return client.useFormgroupUsageCount();
};

exports.useFormgroupUsageCounts = useFormgroupUsageCounts;

var useFormHandlers = function useFormHandlers() {
  return client.useFormHandlers();
};

exports.useFormHandlers = useFormHandlers;

var useStandardizedForm = function useStandardizedForm(formId) {
  return client.getFormStandardized({
    formId: formId
  });
};

exports.useStandardizedForm = useStandardizedForm;

function useMutations() {
  // Trigger refetching paths on update. Only the paths that are currently
  // loaded by the app, will be refetched.
  var refetch = function refetch(response) {
    return Promise.all(response);
  };

  var refetchForms = function refetchForms(id) {
    return refetch([client.refetchForm({
      formId: String(id)
    }), client.refetchForms(), client.refetchDraftForms(), client.refetchPublishedForms(), client.refetchPublishedPublicForms(), client.refetchFormTags()]);
  };

  var refetchFormGroups = function refetchFormGroups(id) {
    return refetch([client.refetchFormGroup({
      formGroupId: String(id)
    }), client.refetchFormGroups()]);
  };

  var refetchFields = function refetchFields(id) {
    return refetch([client.refetchField({
      fieldId: String(id)
    }), client.refetchFields()]);
  };

  return {
    submit: function submit(data, credentials, formDataAction, isDraft, layer) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (!(data._id && (isDraft && data.status === "DRAFT" || !isDraft && data.status === "SUBMITTED"))) {
                  _context.next = 4;
                  break;
                }

                return _context.abrupt("return", client.postResubmitFormData({
                  dataId: data._id,
                  credentials: credentials,
                  actionId: formDataAction,
                  isDraft: isDraft,
                  layer: layer
                }, data));

              case 4:
                if (!(isDraft && data.status === "SUBMITTED")) {
                  _context.next = 8;
                  break;
                }

                return _context.abrupt("return", client.postFormData({
                  actionId: formDataAction,
                  credentials: credentials,
                  draftReference: data._id,
                  layer: layer
                }, data));

              case 8:
                return _context.abrupt("return", client.postFormData({
                  actionId: formDataAction,
                  credentials: credentials,
                  layer: layer
                }, data));

              case 9:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    },
    postForm: function postForm(data) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return client.postForm(null, (0, _legacyMode.handleLegacyConfig)(data));

              case 2:
                _context2.next = 4;
                return refetchForms(data._id);

              case 4:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }))();
    },
    putForm: function putForm(data) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
        return _regenerator["default"].wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.next = 2;
                return client.putForm(null, (0, _legacyMode.handleLegacyConfig)(data));

              case 2:
                _context3.next = 4;
                return refetchForms(data._id);

              case 4:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      }))();
    },
    deleteForm: function deleteForm(formId) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
        return _regenerator["default"].wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _context4.next = 2;
                return client.deleteForm({
                  formId: formId
                });

              case 2:
                _context4.next = 4;
                return refetchForms(formId);

              case 4:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4);
      }))();
    },
    postFormGroup: function postFormGroup(data) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5() {
        return _regenerator["default"].wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                _context5.next = 2;
                return client.postFormGroup(null, (0, _legacyMode.handleLegacyConfig)(data));

              case 2:
                _context5.next = 4;
                return refetchFormGroups(data._id);

              case 4:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5);
      }))();
    },
    putFormGroup: function putFormGroup(data) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6() {
        return _regenerator["default"].wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                _context6.next = 2;
                return client.putFormGroup(null, (0, _legacyMode.handleLegacyConfig)(data));

              case 2:
                _context6.next = 4;
                return refetchFormGroups(data._id);

              case 4:
              case "end":
                return _context6.stop();
            }
          }
        }, _callee6);
      }))();
    },
    deleteFormGroup: function deleteFormGroup(formGroupId) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7() {
        return _regenerator["default"].wrap(function _callee7$(_context7) {
          while (1) {
            switch (_context7.prev = _context7.next) {
              case 0:
                _context7.next = 2;
                return client.deleteFormGroup({
                  formGroupId: formGroupId
                });

              case 2:
                _context7.next = 4;
                return refetchFormGroups(formGroupId);

              case 4:
              case "end":
                return _context7.stop();
            }
          }
        }, _callee7);
      }))();
    },
    postField: function postField(data) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8() {
        var response;
        return _regenerator["default"].wrap(function _callee8$(_context8) {
          while (1) {
            switch (_context8.prev = _context8.next) {
              case 0:
                _context8.next = 2;
                return client.postField(null, (0, _legacyMode.handleLegacyConfig)(data));

              case 2:
                response = _context8.sent;
                _context8.next = 5;
                return refetchFields(data._id);

              case 5:
                return _context8.abrupt("return", response);

              case 6:
              case "end":
                return _context8.stop();
            }
          }
        }, _callee8);
      }))();
    },
    putField: function putField(data) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9() {
        return _regenerator["default"].wrap(function _callee9$(_context9) {
          while (1) {
            switch (_context9.prev = _context9.next) {
              case 0:
                _context9.next = 2;
                return client.putField(null, (0, _legacyMode.handleLegacyConfig)(data));

              case 2:
                _context9.next = 4;
                return refetchFields(data._id);

              case 4:
              case "end":
                return _context9.stop();
            }
          }
        }, _callee9);
      }))();
    },
    deleteField: function deleteField(fieldId) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10() {
        return _regenerator["default"].wrap(function _callee10$(_context10) {
          while (1) {
            switch (_context10.prev = _context10.next) {
              case 0:
                _context10.next = 2;
                return client.deleteField({
                  fieldId: fieldId
                });

              case 2:
                _context10.next = 4;
                return refetchFields(fieldId);

              case 4:
              case "end":
                return _context10.stop();
            }
          }
        }, _callee10);
      }))();
    },
    importData: function importData(data, usePortkeys) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee11() {
        return _regenerator["default"].wrap(function _callee11$(_context11) {
          while (1) {
            switch (_context11.prev = _context11.next) {
              case 0:
                _context11.next = 2;
                return client.postConfigs({
                  usePortkeys: usePortkeys
                }, data);

              case 2:
              case "end":
                return _context11.stop();
            }
          }
        }, _callee11);
      }))();
    },
    updateConfig: function updateConfig(data) {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee12() {
        return _regenerator["default"].wrap(function _callee12$(_context12) {
          while (1) {
            switch (_context12.prev = _context12.next) {
              case 0:
                _context12.next = 2;
                return client.postConfig(null, data);

              case 2:
              case "end":
                return _context12.stop();
            }
          }
        }, _callee12);
      }))();
    }
  };
}
//# sourceMappingURL=api.cjs.map