'use strict';

var i18next = require('i18next');
var v2_constants_regex = require('../../constants/regex.js');
var yup = require('yup');

function _interopNamespaceDefault(e) {
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var yup__namespace = /*#__PURE__*/_interopNamespaceDefault(yup);

var slugValidator = function slugValidator(label) {
  return yup__namespace.string().test({
    name: "slug",
    test: function test(value, ctx) {
      if (!value) {
        return ctx.createError({
          message: i18next.t("neetoCommons.validators.isRequired", {
            what: label
          })
        });
      }
      if (v2_constants_regex.VALID_SLUG_REGEX.test(value)) return true;
      if (/[A-Z]/.test(value)) {
        return ctx.createError({
          message: i18next.t("neetoCommons.validators.mustNotContainCapitalLetters", {
            what: label
          })
        });
      }
      if (value !== null && value !== void 0 && value.includes(" ")) {
        return ctx.createError({
          message: i18next.t("neetoCommons.validators.mustNotContainSpaces", {
            what: label
          })
        });
      }
      if (!/^[a-z0-9-]+$/.test(value)) {
        return ctx.createError({
          message: i18next.t("neetoCommons.validators.mustNotContainSpecialCharactersExceptHyphen", {
            what: label
          })
        });
      }
      if (/^[-]|[-]$/.test(value)) {
        return ctx.createError({
          message: i18next.t("neetoCommons.validators.mustNotStartOrEndWithSpecialCharacters", {
            what: label
          })
        });
      }
      return true;
    }
  });
};

exports.slugValidator = slugValidator;
//# sourceMappingURL=slug.js.map
