const metodosFormularios = {};

metodosFormularios.validar = ({ id }) => {
  let formulario = document.querySelector(`#${id}`);

  if (!!formulario) {
    return $(`#${id}`)[0].checkValidity();
  }

  return false;
};

metodosFormularios.limpiar = ({ id }) => {
  return new Promise((resolve, reject) => {
    try {
      let formulario = document.querySelector(`#${id}`);
      formulario.reset();
      formulario.classList.remove("was-validated");

      [
        ...formulario.querySelectorAll(
          `select[data-special-selectpicker="true"]`
        ),
      ].forEach((select) => {
        $(`#${select.id}`).selectpicker("destroy");
        $(`#${select.id}`).selectpicker();
      });
      [
        ...formulario.querySelectorAll(`select[data-live-search="true"]`),
      ].forEach((select) => {
        $(`#${select.id}`).selectpicker("destroy");
        $(`#${select.id}`).selectpicker();
      });

      resolve();
    } catch (err) {
      reject(err);
    }
  });
};

export default metodosFormularios;
