#![cfg(test)]

use js_sys::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "tests/wasm/JsString.js")]
extern "C" {
    fn new_string_object() -> JsValue;
    fn get_replacer_function() -> Function;
}

#[wasm_bindgen_test]
fn js_string_inheritance() {
    let string = new_string_object();
    assert!(string.is_instance_of::<JsString>());
    assert!(string.is_instance_of::<Object>());
}

#[wasm_bindgen_test]
fn length() {
    fn test(s: &str) {
        assert_eq!(JsString::from(s).length(), s.len() as u32);
    }
    test("Mozilla");
    test("");
}

#[wasm_bindgen_test]
fn char_at() {
    let s = JsString::from("Brave new world");
    assert_eq!(JsValue::from(s.char_at(0)), "B");
    assert_eq!(JsValue::from(s.char_at(999)), "");
}

#[wasm_bindgen_test]
fn char_code_at() {
    let s = "Brave new world";
    let js = JsString::from(s);
    for (i, b) in s.char_indices() {
        assert_eq!(js.char_code_at(i as u32), b as u32 as f64);
    }
    assert!(js.char_code_at(s.len() as u32).is_nan());
}

#[wasm_bindgen_test]
fn code_point_at() {
    assert_eq!(JsString::from("ABC").code_point_at(1), b'B');
    assert!(JsString::from("ABC").code_point_at(42).is_undefined());
}

#[wasm_bindgen_test]
fn concat() {
    // TODO: Implement ability to receive multiple optional arguments
    let s = JsString::from("Hello ").concat(&"World".into());
    assert_eq!(JsValue::from(s), "Hello World");
    let foo = JsString::from("foo");
    assert_eq!(
        JsValue::from(foo.concat(&Object::new().into())),
        "foo[object Object]"
    );
    assert_eq!(JsValue::from(foo.concat(&Array::new().into())), "foo");
    assert_eq!(JsValue::from(foo.concat(&JsValue::null())), "foonull");
    assert_eq!(JsValue::from(foo.concat(&true.into())), "footrue");
    assert_eq!(JsValue::from(foo.concat(&1234.into())), "foo1234");
}

#[wasm_bindgen_test]
fn ends_with() {
    let s = "To be, or not to be, that is the question.";
    let js = JsString::from(s);

    // TODO: remove third parameter once we have optional parameters
    assert!(js.ends_with("question.", s.len() as i32));
    assert!(!js.ends_with("to be", s.len() as i32));
    assert!(js.ends_with("to be", 19));
}

#[wasm_bindgen_test]
fn from_char_code() {
    let s = "½+¾=";
    let codes: Vec<u32> = s.chars().map(|char| char as u32).collect();

    assert_eq!(JsString::from_char_code1(codes[0]), "½");
    assert_eq!(JsString::from_char_code2(codes[0], codes[1]), "½+");
    assert_eq!(
        JsString::from_char_code3(codes[0], codes[1], codes[2]),
        "½+¾"
    );
    assert_eq!(
        JsString::from_char_code4(codes[0], codes[1], codes[2], codes[3]),
        "½+¾="
    );

    let codes_u16: Vec<u16> = codes
        .into_iter()
        .map(|code| {
            assert!(code <= u32::from(u16::MAX));
            code as u16
        })
        .collect();

    assert_eq!(JsString::from_char_code(&codes_u16), "½+¾=");
}

#[wasm_bindgen_test]
fn from_code_point() {
    let s = "☃★♲你";
    let codes: Vec<u32> = s.chars().map(|char| char as u32).collect();

    assert_eq!(JsString::from_code_point1(codes[0]).unwrap(), "☃");
    assert_eq!(
        JsString::from_code_point2(codes[0], codes[1]).unwrap(),
        "☃★"
    );
    assert_eq!(
        JsString::from_code_point3(codes[0], codes[1], codes[2]).unwrap(),
        "☃★♲"
    );
    assert_eq!(
        JsString::from_code_point4(codes[0], codes[1], codes[2], codes[3]).unwrap(),
        "☃★♲你"
    );
    assert_eq!(JsString::from_code_point(&codes).unwrap(), "☃★♲你");

    assert!(JsString::from_code_point1(0x10FFFF).is_ok());
    assert!(JsString::from_code_point1(0x110000).is_err());
    assert!(JsString::from_code_point1(u32::MAX).is_err());
}

#[wasm_bindgen_test]
fn includes() {
    let str = JsString::from("Blue Whale");

    // TODO: remove second parameter once we have optional parameters
    assert!(str.includes("Blue", 0));
    assert!(!str.includes("Blute", 0));
    assert!(str.includes("Whale", 0));
    assert!(str.includes("Whale", 5));
    assert!(!str.includes("Whale", 7));
    assert!(str.includes("", 0));
    assert!(str.includes("", 16));
}

#[wasm_bindgen_test]
fn index_of() {
    let str = JsString::from("Blue Whale");

    // TODO: remove second parameter once we have optional parameters
    assert_eq!(str.index_of("Blue", 0), 0);
    // TODO: remove second parameter once we have optional parameters
    assert_eq!(str.index_of("Blute", 0), -1);
    assert_eq!(str.index_of("Whale", 0), 5);
    assert_eq!(str.index_of("Whale", 5), 5);
    assert_eq!(str.index_of("Whale", 7), -1);
    // TODO: remove second parameter once we have optional parameters
    assert_eq!(str.index_of("", 0), 0);
    assert_eq!(str.index_of("", 9), 9);
    assert_eq!(str.index_of("", 10), 10);
    assert_eq!(str.index_of("", 11), 10);
}

#[wasm_bindgen_test]
fn last_index_of() {
    let js = JsString::from("canal");
    let len = js.length() as i32;

    // TODO: remove second parameter once we have optional parameters
    assert_eq!(js.last_index_of("a", len), 3);
    assert_eq!(js.last_index_of("a", 2), 1);
    assert_eq!(js.last_index_of("a", 0), -1);
    // TODO: remove second parameter once we have optional parameters
    assert_eq!(js.last_index_of("x", len), -1);
    assert_eq!(js.last_index_of("c", -5), 0);
    assert_eq!(js.last_index_of("c", 0), 0);
    // TODO: remove second parameter once we have optional parameters
    assert_eq!(js.last_index_of("", len), 5);
    assert_eq!(js.last_index_of("", 2), 2);
}

#[wasm_bindgen_test]
fn match_() {
    let s = "The quick brown fox jumped over the lazy dog. It barked.";
    let re = RegExp::new("[A-Z]", "g");
    let result = JsString::from(s).match_(&re);
    let obj = result.unwrap();

    assert_eq!(Reflect::get(obj.as_ref(), &"0".into()).unwrap(), "T");
    assert_eq!(Reflect::get(obj.as_ref(), &"1".into()).unwrap(), "I");

    let re = RegExp::new("[A-Z]([a-z]*)", "g");
    let result = JsString::from(s).match_(&re);
    let obj = result.unwrap();

    assert_eq!(Reflect::get(obj.as_ref(), &"0".into()).unwrap(), "The");
    assert_eq!(Reflect::get(obj.as_ref(), &"1".into()).unwrap(), "It");

    let result = JsString::from("foo").match_(&re);
    assert!(result.is_none());

    let s = "For more information, see Chapter 3.4.5.1";
    let re = RegExp::new("see (chapter \\d+(\\.\\d)*)", "i");
    let result = JsString::from(s).match_(&re);
    let obj = result.unwrap();

    assert_eq!(
        Reflect::get(obj.as_ref(), &"0".into()).unwrap(),
        "see Chapter 3.4.5.1"
    );
    assert_eq!(
        Reflect::get(obj.as_ref(), &"1".into()).unwrap(),
        "Chapter 3.4.5.1"
    );
    assert_eq!(Reflect::get(obj.as_ref(), &"2".into()).unwrap(), ".1");
    assert_eq!(Reflect::get(obj.as_ref(), &"index".into()).unwrap(), 22);
    assert_eq!(Reflect::get(obj.as_ref(), &"input".into()).unwrap(), s);
}

#[wasm_bindgen_test]
fn match_all() {
    let s = "The quick brown fox jumped over the lazy dog. It barked.";
    let re = RegExp::new("[A-Z]([a-z]*)", "g");
    let result: Vec<_> = JsString::from(s)
        .match_all(&re)
        .into_iter()
        .collect::<Result<_, _>>()
        .unwrap();

    let obj = &result[0];
    assert_eq!(Reflect::get(obj, &"0".into()).unwrap(), "The");
    assert_eq!(Reflect::get(obj, &"1".into()).unwrap(), "he");

    let obj = &result[1];
    assert_eq!(Reflect::get(obj, &"0".into()).unwrap(), "It");
    assert_eq!(Reflect::get(obj, &"1".into()).unwrap(), "t");

    let result: Vec<_> = JsString::from("foo")
        .match_all(&re)
        .into_iter()
        .collect::<Result<_, _>>()
        .unwrap();
    assert_eq!(result.len(), 0);

    let s = "For more information, see Chapter 3.4.5.1. Also see Chapter 3.1.4";
    let re = RegExp::new("see (chapter \\d+(\\.\\d)*)", "gi");
    let result: Vec<_> = JsString::from(s)
        .match_all(&re)
        .into_iter()
        .collect::<Result<_, _>>()
        .unwrap();

    let obj = &result[0];
    assert_eq!(
        Reflect::get(obj, &"0".into()).unwrap(),
        "see Chapter 3.4.5.1"
    );
    assert_eq!(Reflect::get(obj, &"1".into()).unwrap(), "Chapter 3.4.5.1");
    assert_eq!(Reflect::get(obj, &"2".into()).unwrap(), ".1");
    assert_eq!(Reflect::get(obj, &"index".into()).unwrap(), 22);
    assert_eq!(Reflect::get(obj, &"input".into()).unwrap(), s);

    let obj = &result[1];
    assert_eq!(Reflect::get(obj, &"0".into()).unwrap(), "see Chapter 3.1.4");
    assert_eq!(Reflect::get(obj, &"1".into()).unwrap(), "Chapter 3.1.4");
    assert_eq!(Reflect::get(obj, &"2".into()).unwrap(), ".4");
    assert_eq!(Reflect::get(obj, &"index".into()).unwrap(), 48);
    assert_eq!(Reflect::get(obj, &"input".into()).unwrap(), s);
}

#[wasm_bindgen_test]
fn normalize() {
    let js = JsString::from("\u{1E9B}\u{0323}");

    // TODO: Handle undefined
    assert_eq!(JsValue::from(js.normalize("NFC")), "\u{1E9B}\u{0323}");
    assert_eq!(
        JsValue::from(js.normalize("NFD")),
        "\u{017F}\u{0323}\u{0307}"
    );
    assert_eq!(JsValue::from(js.normalize("NFKC")), "\u{1E69}");
    assert_eq!(
        JsValue::from(js.normalize("NFKD")),
        "\u{0073}\u{0323}\u{0307}"
    );
}

#[wasm_bindgen_test]
fn pad_end() {
    let js = JsString::from("abc");

    // TODO: remove second parameter once we have optional parameters
    assert_eq!(JsValue::from(js.pad_end(10, " ")), "abc       ");
    // TODO: remove second parameter once we have optional parameters
    assert_eq!(JsValue::from(js.pad_end(10, " ")), "abc       ");
    assert_eq!(JsValue::from(js.pad_end(10, "foo")), "abcfoofoof");
    assert_eq!(JsValue::from(js.pad_end(6, "123456")), "abc123");
    // TODO: remove second parameter once we have optional parameters
    assert_eq!(JsValue::from(js.pad_end(1, " ")), "abc");
}

#[wasm_bindgen_test]
fn pad_start() {
    let js = JsString::from("abc");

    // TODO: remove second parameter once we have optional parameters
    assert_eq!(js.pad_start(10, " "), "       abc");
    assert_eq!(js.pad_start(10, "foo"), "foofoofabc");
    assert_eq!(js.pad_start(6, "123465"), "123abc");
    assert_eq!(js.pad_start(8, "0"), "00000abc");
    // TODO: remove second parameter once we have optional parameters
    assert_eq!(js.pad_start(1, " "), "abc");
}

#[wasm_bindgen_test]
fn repeat() {
    assert_eq!(JsString::from("test").repeat(3), "testtesttest");
}

#[wasm_bindgen_test]
fn replace() {
    let js = JsString::from(
        "The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?",
    );
    let result = js.replace("dog", "ferret");

    assert_eq!(
        result,
        "The quick brown fox jumped over the lazy ferret. If the dog reacted, was it really lazy?"
    );

    let js = JsString::from("borderTop");
    let result = js.replace_with_function("T", &get_replacer_function());

    assert_eq!(result, "border-top");

    let js = JsString::from(
        "The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?",
    );
    let re = RegExp::new("dog", "g");
    let result = js.replace_by_pattern(&re, "ferret");

    assert_eq!(result, "The quick brown fox jumped over the lazy ferret. If the ferret reacted, was it really lazy?");

    let js = JsString::from("borderTop");
    let re = RegExp::new("[A-Z]", "g");
    let result = js.replace_by_pattern_with_function(&re, &get_replacer_function());

    assert_eq!(result, "border-top");
}

#[wasm_bindgen_test]
fn replace_all() {
    let js = JsString::from(
        "The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?",
    );
    let result = js.replace_all("dog", "ferret");

    assert_eq!(
        result,
        "The quick brown fox jumped over the lazy ferret. If the ferret reacted, was it really lazy?"
    );

    let js = JsString::from("borderTopTest");
    let result = js.replace_all_with_function("T", &get_replacer_function());

    assert_eq!(result, "border-top-test");

    let js = JsString::from(
        "The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?",
    );
    let re = RegExp::new("dog", "g");
    let result = js.replace_all_by_pattern(&re, "ferret");

    assert_eq!(result, "The quick brown fox jumped over the lazy ferret. If the ferret reacted, was it really lazy?");

    let js = JsString::from("borderTopTest");
    let re = RegExp::new("[A-Z]", "g");
    let result = js.replace_all_by_pattern_with_function(&re, &get_replacer_function());

    assert_eq!(result, "border-top-test");
}

#[wasm_bindgen_test]
fn search() {
    let js = JsString::from(
        "The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?",
    );
    let re = RegExp::new("[^\\w\\s]", "g");

    assert_eq!(js.search(&re), 44);

    let js = JsString::from("hey JudE");
    let re1 = RegExp::new("[A-Z]", "g");
    let re2 = RegExp::new("[.]", "g");

    assert_eq!(js.search(&re1), 4);
    assert_eq!(js.search(&re2), -1);
}

#[wasm_bindgen_test]
fn slice() {
    let characters = JsString::from("acxn18");
    assert_eq!(characters.slice(1, 3), "cx");
}

#[wasm_bindgen_test]
fn split() {
    let js = JsString::from("Oh brave new world");
    let result = js.split(" ");

    let mut v = Vec::with_capacity(result.length() as usize);
    result.for_each(&mut |x, _, _| v.push(x));

    assert_eq!(v[0], "Oh");
    assert_eq!(v[1], "brave");
    assert_eq!(v[2], "new");
    assert_eq!(v[3], "world");

    let js = JsString::from("Oct,Nov,Dec");
    let result = js.split(",");

    let mut v = Vec::with_capacity(result.length() as usize);
    result.for_each(&mut |x, _, _| v.push(x));

    assert_eq!(v[0], "Oct");
    assert_eq!(v[1], "Nov");
    assert_eq!(v[2], "Dec");

    let result = js.split_limit(",", 2);

    let mut v = Vec::with_capacity(result.length() as usize);
    result.for_each(&mut |x, _, _| v.push(x));

    assert_eq!(result.length(), 2);
    assert_eq!(v[0], "Oct");
    assert_eq!(v[1], "Nov");

    let js = JsString::from("Oh brave new world");
    let re = RegExp::new("\\s", "g");
    let result = js.split_by_pattern(&re);

    let mut v = Vec::with_capacity(result.length() as usize);
    result.for_each(&mut |x, _, _| v.push(x));

    assert_eq!(v[0], "Oh");
    assert_eq!(v[1], "brave");
    assert_eq!(v[2], "new");
    assert_eq!(v[3], "world");

    let result = js.split_by_pattern_limit(&re, 2);

    let mut v = Vec::with_capacity(result.length() as usize);
    result.for_each(&mut |x, _, _| v.push(x));

    assert_eq!(result.length(), 2);
    assert_eq!(v[0], "Oh");
    assert_eq!(v[1], "brave");
}

#[wasm_bindgen_test]
fn starts_with() {
    let js = JsString::from("To be, or not to be, that is the question.");

    // TODO: remove second parameter for both assertions once we have optional parameters
    assert!(js.starts_with("To be", 0));
    assert!(!js.starts_with("not to be", 0));
    assert!(js.starts_with("not to be", 10));
}

#[wasm_bindgen_test]
fn substring() {
    let js = JsString::from("Mozilla");

    assert_eq!(js.substring(0, 1), "M");
    assert_eq!(js.substring(1, 0), "M");

    assert_eq!(js.substring(0, 6), "Mozill");

    // TODO: Add test once we have optional parameters
    // assert_eq!(js.substring(4), "lla");
    assert_eq!(js.substring(4, 7), "lla");
    assert_eq!(js.substring(7, 4), "lla");

    assert_eq!(js.substring(0, 7), "Mozilla");
    assert_eq!(js.substring(0, 10), "Mozilla");
}

#[wasm_bindgen_test]
fn substr() {
    let js = JsString::from("Mozilla");

    assert_eq!(js.substr(0, 1), "M");
    assert_eq!(js.substr(1, 0), "");
    assert_eq!(js.substr(-1, 1), "a");
    assert_eq!(js.substr(1, -1), "");
    // TODO: Uncomment and test these assertions, once we have support for optional parameters
    // assert_eq!(js.substr(-3), "lla");
    // assert_eq!(js.substr(1), "ozilla");
    assert_eq!(js.substr(-20, 2), "Mo");
    assert_eq!(js.substr(20, 2), "");
}

#[wasm_bindgen_test]
fn to_locale_lower_case() {
    let js = JsString::from("Mozilla");
    assert_eq!(js.to_locale_lower_case(None), "mozilla");
    let s = JsString::from("\u{0130}");
    assert_eq!(s.to_locale_lower_case(Some("tr")), "i");
    assert_ne!(s.to_locale_lower_case(Some("en-US")), "i");
}

#[wasm_bindgen_test]
fn to_locale_upper_case() {
    let js = JsString::from("mozilla");
    assert_eq!(js.to_locale_upper_case(None), "MOZILLA");
    let s = JsString::from("i\u{0307}");
    assert_eq!(s.to_locale_upper_case(Some("lt")), "I");
    assert_ne!(s.to_locale_upper_case(Some("en-US")), "I");
}

#[wasm_bindgen_test]
fn to_lower_case() {
    assert_eq!(JsString::from("Mozilla").to_lower_case(), "mozilla");
}

#[wasm_bindgen_test]
fn to_string() {
    assert_eq!(JsString::from("foo").to_string(), "foo");
}

#[wasm_bindgen_test]
fn to_upper_case() {
    assert_eq!(JsString::from("Mozilla").to_upper_case(), "MOZILLA");
}

#[wasm_bindgen_test]
fn trim() {
    assert_eq!(JsString::from("   foo  ").trim(), "foo");
    // Another example of .trim() removing whitespace from just one side.
    assert_eq!(JsString::from("foo   ").trim(), "foo");
}

#[wasm_bindgen_test]
fn trim_end_and_trim_right() {
    let greeting = JsString::from("   Hello world!   ");
    let trimmed = "   Hello world!";
    assert_eq!(greeting.trim_end(), trimmed);
    assert_eq!(greeting.trim_right(), trimmed);
}

#[wasm_bindgen_test]
fn trim_start_and_trim_left() {
    let greeting = JsString::from("   Hello world!   ");
    let trimmed = "Hello world!   ";
    assert_eq!(greeting.trim_start(), trimmed);
    assert_eq!(greeting.trim_left(), trimmed);
}

#[wasm_bindgen_test]
fn value_of() {
    let greeting = JsString::from("Hello world!");
    assert_eq!(greeting.value_of(), "Hello world!");
}

#[wasm_bindgen_test]
fn raw() {
    let call_site = Object::new();
    let raw = Array::of3(&"foo".into(), &"bar".into(), &"123".into());
    Reflect::set(call_site.as_ref(), &"raw".into(), &raw.into()).unwrap();
    assert_eq!(
        JsString::raw_2(&call_site, "5", "JavaScript").unwrap(),
        "foo5barJavaScript123"
    );
    let substitutions = Array::of2(&"5".into(), &"JavaScript".into());
    assert_eq!(
        JsString::raw(&call_site, &substitutions).unwrap(),
        "foo5barJavaScript123"
    );
    assert!(JsString::raw_0(&JsValue::null().unchecked_into()).is_err());
}

#[wasm_bindgen_test]
fn is_valid_utf16() {
    assert!(JsString::from("a").is_valid_utf16());
    assert!(JsString::from("").is_valid_utf16());
    assert!(JsString::from("🥑").is_valid_utf16());
    assert!(JsString::from("Why hello there this, 🥑, is 🥑 and is 🥑").is_valid_utf16());

    assert!(JsString::from_char_code1(0x00).is_valid_utf16());
    assert!(!JsString::from_char_code1(0xd800).is_valid_utf16());
    assert!(!JsString::from_char_code1(0xdc00).is_valid_utf16());
}

#[wasm_bindgen_test]
fn as_char() {
    assert_eq!(JsString::from('a').as_char(), Some('a'));
    assert_eq!(JsString::from('🥑').as_char(), Some('🥑'));
    assert_eq!(JsString::from("").as_char(), None);
    assert_eq!(JsString::from("ab").as_char(), None);
    assert_eq!(JsString::from_char_code1(0xd800).as_char(), None);
    assert_eq!(JsString::from_char_code1(0xdc00).as_char(), None);
    assert_eq!(JsString::from_char_code1(0xdfff).as_char(), None);
}
