import crypto from "crypto"

import { env } from "@/env/server"

export default function encryptValue(originalString: string) {
  try {
    const encryptionKey = env.BOOKING_ENCRYPTION_KEY
    const bufferKey = Buffer.from(encryptionKey, "utf8")
    const cipher = crypto.createCipheriv("DES-ECB", bufferKey, null)
    cipher.setAutoPadding(false)
    const bufferString = Buffer.from(originalString, "utf8")
    const paddingSize =
      bufferKey.length - (bufferString.length % bufferKey.length)
    const paddedStr = Buffer.concat([
      bufferString,
      Buffer.alloc(paddingSize, 0),
    ])
    const buffers: Buffer[] = []
    buffers.push(cipher.update(paddedStr))
    buffers.push(cipher.final())
    const result = Buffer.concat(buffers).toString("base64").replace(/\+/g, "-")
    return result
  } catch (e) {
    console.log(e)
    return ""
  }
}
