It depends on whether the address was hashed from a script or a public key. If the latter then you have a P2SH-P2WPKH address and you generate it the same way as legacy "1" addresses: SHA256 the public key, then RIPEMD160 that, and finally encode it in base58.
If however the address is hashed from the script, then it's a regular P2SH address and you should RIPEMD160 the script bytes to get the script's public key, and then base58 encode that.
I think that particular script you found generates a P2SH address and not a P2SH-P2WPKH Bech32 address. I found a similar version of it at https://bitcoin.stackexchange.com/a/76394/112589 but it's for multisig (P2SH-P2WSH). Maybe you adapted yours from it.