JSFiddle - React, Tailwind, and code Playground

JavaScript

function transposition(arr, prepend) {
  var i, version, el, result = [];
  prepend = prepend || [];
  if(arr.length === 1) return [arr];
  for( i=0; i<arr.length; i++) {
    if( arr.length === 2) {
      result.push( prepend.concat( [arr[i], arr[(i+1)%2]] ));
    } else {
      version = arr.slice();
      el = version.splice(i,1);
      result = result.concat( transposition( version, prepend.concat(el)));
    }
  }
  return result;
}

const nextSmaller = (n) => {
  if (n > 20) {
    let result = transposition( n.toString().split('') );
    let set = new Set();
    result = result.map( item => set.add(item.join('')))
    result = Array.from(set).sort( (a, b) => b - a );
    console.log(result)
    for (let i = 0; i < result.length; i++) {
      if (result[i] < n) {
        return result[i];
      }
    }

  }
  return -1;
}