'use strict';

var pickBy = require('lodash/pickBy');
var errors = require('@backstage/errors');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }

var pickBy__default = /*#__PURE__*/_interopDefaultCompat(pickBy);

function encodeOAuthState(state) {
  const stateString = new URLSearchParams(
    pickBy__default.default(state, (value) => value !== void 0)
  ).toString();
  return Buffer.from(stateString, "utf-8").toString("hex");
}
function decodeOAuthState(encodedState) {
  const state = Object.fromEntries(
    new URLSearchParams(Buffer.from(encodedState, "hex").toString("utf-8"))
  );
  if (!state.env || state.env?.length === 0) {
    throw new errors.NotAllowedError("OAuth state is invalid, missing env");
  }
  if (!state.nonce || state.nonce?.length === 0) {
    throw new errors.NotAllowedError("OAuth state is invalid, missing nonce");
  }
  return state;
}

exports.decodeOAuthState = decodeOAuthState;
exports.encodeOAuthState = encodeOAuthState;
//# sourceMappingURL=state.cjs.js.map
