#![allow(dead_code)]
#![allow(clippy::type_complexity)]
use crate::parser::Link;
use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag;
use percent_encoding::percent_decode_str;
use std::borrow::Cow;
pub fn wikitext_text2dest_link(i: &str) -> nom::IResult<&str, Link> {
    let (i, (te, de, ti)) = wikitext_text2dest(i)?;
    Ok((i, Link::Text2Dest(te, de, ti)))
}
pub fn wikitext_text2dest(i: &str) -> nom::IResult<&str, (Cow<str>, Cow<str>, Cow<str>)> {
    let (i, (link_text, link_destination)) = nom::sequence::delimited(
        tag("["),
        nom::combinator::map_parser(is_not("]\n\r"), parse_inner),
        tag("]"),
    )(i)?;
    Ok((i, (link_text, link_destination, Cow::from(""))))
}
fn parse_inner(i: &str) -> nom::IResult<&str, (Cow<str>, Cow<str>)> {
    let (i, link_destination) = nom::sequence::terminated(
        nom::combinator::map_parser(
            nom::bytes::complete::take_till(|c| c == ' ' || c == '\t'),
            parse_url,
        ),
        nom::character::complete::space0,
    )(i)?;
    let link_text = i;
    Ok((i, (Cow::from(link_text), link_destination)))
}
fn parse_url(i: &str) -> nom::IResult<&str, Cow<str>> {
    nom::combinator::peek(alt((tag("http:"), tag("https:"), tag("mailto:"))))(i)?;
    let url = percent_decode_str(i).decode_utf8().unwrap();
    Ok(("", url))
}
#[test]
fn test_wikitext_text2dest() {
    let expected = (
        "abc",
        (
            Cow::from("W3Schools"),
            Cow::from("https://www.w3schools.com/"),
            Cow::from(""),
        ),
    );
    assert_eq!(
        wikitext_text2dest(r#"[https://www.w3schools.com/ W3Schools]abc"#).unwrap(),
        expected
    );
    assert_eq!(
        wikitext_text2dest(r#"[https://www.w3schools.com/   W3Schools]abc"#).unwrap(),
        expected
    );
    let expected = (
        "abc",
        (
            Cow::from("W3Schools"),
            Cow::from("http://www.w3schools.com/"),
            Cow::from(""),
        ),
    );
    assert_eq!(
        wikitext_text2dest(r#"[http://www.w3schools.com/ W3Schools]abc"#).unwrap(),
        expected
    );
    let expected = (
        "abc",
        (
            Cow::from("W3Schools website"),
            Cow::from("http://www.w3schools.com/"),
            Cow::from(""),
        ),
    );
    assert_eq!(
        wikitext_text2dest(r#"[http://www.w3schools.com/ W3Schools website]abc"#).unwrap(),
        expected
    );
    assert_eq!(
        wikitext_text2dest("[http://www.w3schools.com/\tW3Schools website]abc").unwrap(),
        expected
    );
    let expected = (
        "abc",
        (
            Cow::from(""),
            Cow::from("http://www.w3schools.com/"),
            Cow::from(""),
        ),
    );
    assert_eq!(
        wikitext_text2dest(r#"[http://www.w3schools.com/]abc"#).unwrap(),
        expected
    );
    assert_eq!(
        wikitext_text2dest(r#"[http://www.w3schools.com/ ]abc"#).unwrap(),
        expected
    );
    assert_eq!(
        wikitext_text2dest("[http://www.w3schools.com/\t ]abc").unwrap(),
        expected
    );
    let expected = (
        "abc",
        (
            Cow::from("John Don"),
            Cow::from("mailto:john.don@somemail.com"),
            Cow::from(""),
        ),
    );
    assert_eq!(
        wikitext_text2dest(r#"[mailto:john.don@somemail.com John Don]abc"#).unwrap(),
        expected
    );
    assert_eq!(
        wikitext_text2dest(r#"[httpx://www.w3schools.com/ W3Schools]abc"#).unwrap_err(),
        nom::Err::Error(nom::error::Error::new(
            "httpx://www.w3schools.com/",
            nom::error::ErrorKind::Tag
        ))
    );
}