use core::{fmt, iter, slice, str};
use bitcoin_hashes::{sha256d, Hash};
static BASE58_CHARS: &[u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
#[rustfmt::skip]
static BASE58_DIGITS: [Option<u8>; 128] = [
    None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     None,     Some(0),  Some(1),  Some(2),  Some(3),  Some(4),  Some(5),  Some(6),  Some(7),  Some(8),  None,     None,     None,     None,     None,     None,     None,     Some(9),  Some(10), Some(11), Some(12), Some(13), Some(14), Some(15), Some(16), None,     Some(17), Some(18), Some(19), Some(20), Some(21), None,     Some(22), Some(23), Some(24), Some(25), Some(26), Some(27), Some(28), Some(29), Some(30), Some(31), Some(32), None,     None,     None,     None,     None,     None,     Some(33), Some(34), Some(35), Some(36), Some(37), Some(38), Some(39), Some(40), Some(41), Some(42), Some(43), None,     Some(44), Some(45), Some(46), Some(47), Some(48), Some(49), Some(50), Some(51), Some(52), Some(53), Some(54), Some(55), Some(56), Some(57), None,     None,     None,     None,     None,     ];
pub fn decode(data: &str) -> Result<Vec<u8>, Error> {
    let mut scratch = vec![0u8; 1 + data.len() * 11 / 15];
    for d58 in data.bytes() {
        if d58 as usize >= BASE58_DIGITS.len() {
            return Err(Error::BadByte(d58));
        }
        let mut carry = match BASE58_DIGITS[d58 as usize] {
            Some(d58) => d58 as u32,
            None => {
                return Err(Error::BadByte(d58));
            }
        };
        for d256 in scratch.iter_mut().rev() {
            carry += *d256 as u32 * 58;
            *d256 = carry as u8;
            carry /= 256;
        }
        assert_eq!(carry, 0);
    }
    let mut ret: Vec<u8> = data.bytes().take_while(|&x| x == BASE58_CHARS[0]).map(|_| 0).collect();
    ret.extend(scratch.into_iter().skip_while(|&x| x == 0));
    Ok(ret)
}
pub fn decode_check(data: &str) -> Result<Vec<u8>, Error> {
    let mut ret: Vec<u8> = decode(data)?;
    if ret.len() < 4 {
        return Err(Error::TooShort(ret.len()));
    }
    let check_start = ret.len() - 4;
    let hash_check =
        sha256d::Hash::hash(&ret[..check_start])[..4].try_into().expect("4 byte slice");
    let data_check = ret[check_start..].try_into().expect("4 byte slice");
    let expected = u32::from_le_bytes(hash_check);
    let actual = u32::from_le_bytes(data_check);
    if expected != actual {
        return Err(Error::BadChecksum(expected, actual));
    }
    ret.truncate(check_start);
    Ok(ret)
}
pub fn encode(data: &[u8]) -> String { encode_iter(data.iter().cloned()) }
pub fn encode_check(data: &[u8]) -> String {
    let checksum = sha256d::Hash::hash(data);
    encode_iter(data.iter().cloned().chain(checksum[0..4].iter().cloned()))
}
pub fn encode_check_to_fmt(fmt: &mut fmt::Formatter, data: &[u8]) -> fmt::Result {
    let checksum = sha256d::Hash::hash(data);
    let iter = data.iter().cloned().chain(checksum[0..4].iter().cloned());
    format_iter(fmt, iter)
}
fn encode_iter<I>(data: I) -> String
where I: Iterator<Item = u8> + Clone {
    let mut ret = String::new();
    format_iter(&mut ret, data).expect("writing into string shouldn't fail");
    ret
}
fn format_iter<I, W>(writer: &mut W, data: I) -> Result<(), fmt::Error>
where
    I: Iterator<Item = u8> + Clone,
    W: fmt::Write,
{
    let mut ret = SmallVec::new();
    let mut leading_zero_count = 0;
    let mut leading_zeroes = true;
    for d256 in data {
        let mut carry = d256 as usize;
        if leading_zeroes && carry == 0 {
            leading_zero_count += 1;
        } else {
            leading_zeroes = false;
        }
        for ch in ret.iter_mut() {
            let new_ch = *ch as usize * 256 + carry;
            *ch = (new_ch % 58) as u8;
            carry = new_ch / 58;
        }
        while carry > 0 {
            ret.push((carry % 58) as u8);
            carry /= 58;
        }
    }
    for _ in 0..leading_zero_count {
        ret.push(0);
    }
    for ch in ret.iter().rev() {
        writer.write_char(BASE58_CHARS[*ch as usize] as char)?;
    }
    Ok(())
}
struct SmallVec<T> {
    len: usize,
    stack: [T; 100],
    heap: Vec<T>,
}
impl<T: Default + Copy> SmallVec<T> {
    fn new() -> SmallVec<T> {
        SmallVec {
            len: 0,
            stack: [T::default(); 100],
            heap: Vec::new(),
        }
    }
    fn push(&mut self, val: T) {
        if self.len < 100 {
            self.stack[self.len] = val;
            self.len += 1;
        } else {
            self.heap.push(val);
        }
    }
    fn iter(&self) -> iter::Chain<slice::Iter<T>, slice::Iter<T>> {
        self.stack[0..self.len].iter().chain(self.heap.iter())
    }
    fn iter_mut(&mut self) -> iter::Chain<slice::IterMut<T>, slice::IterMut<T>> {
        self.stack[0..self.len].iter_mut().chain(self.heap.iter_mut())
    }
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub enum Error {
    BadByte(u8),
    BadChecksum(u32, u32),
    InvalidLength(usize),
    InvalidExtendedKeyVersion([u8; 4]),
    InvalidAddressVersion(u8),
    TooShort(usize),
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::BadByte(b) => write!(f, "invalid base58 character {:#x}", b),
            Error::BadChecksum(exp, actual) => {
                write!(f, "base58ck checksum {:#x} does not match expected {:#x}", actual, exp)
            }
            Error::InvalidLength(ell) => write!(f, "length {} invalid for this base58 type", ell),
            Error::InvalidExtendedKeyVersion(ref v) => {
                write!(f, "extended key version {:#04x?} is invalid for this base58 type", v)
            }
            Error::InvalidAddressVersion(ref v) => {
                write!(f, "address version {} is invalid for this base58 type", v)
            }
            Error::TooShort(_) => write!(f, "base58ck data not even long enough for a checksum"),
        }
    }
}