import { convertToDateFormat, isValidDate } from "./validation";

describe("convertToDateFormat", () => {
  it("converts a date from yyyy-mm-dd to yyyy-mm-dd format when no custom format is supplied", () => {
    expect(convertToDateFormat("2022-01-25")).toBe("2022-01-25");
  });
  it("converts a date from yyyy-mm-dd to yyyy-mm-dd format", () => {
    expect(convertToDateFormat("2022-01-25", "yyyy-mm-dd")).toBe("2022-01-25");
  });

  it("converts a date from dd/mm/yyyy to yyyy-mm-dd format", () => {
    expect(convertToDateFormat("25/01/2022", "dd/mm/yyyy")).toBe("2022-01-25");
  });

  it("converts a date from mm/dd/yyyy to yyyy-mm-dd format", () => {
    expect(convertToDateFormat("01/25/2022", "mm/dd/yyyy")).toBe("2022-01-25");
  });

  it("handles single digit days and months with padding", () => {
    expect(convertToDateFormat("5/6/2022", "dd/mm/yyyy")).toBe("2022-06-05");
    expect(convertToDateFormat("6/5/2022", "mm/dd/yyyy")).toBe("2022-06-05");
  });

  it("handles missing parts by using default values", () => {
    expect(convertToDateFormat("2022-01", "yyyy-mm")).toBe("2022-01-00");
    expect(convertToDateFormat("01/2022", "mm/yyyy")).toBe("2022-01-00");
  });

  it("handles empty string by using default values", () => {
    expect(convertToDateFormat("", "yyyy-mm-dd")).toBe("0-00-00");
  });
});

describe("isValidDate", () => {
  it("returns true for undefined date", () => {
    expect(isValidDate(undefined)).toBe(true);
  });

  it("returns true for valid date in default format yyyy-mm-dd", () => {
    expect(isValidDate("2022-01-25")).toBe(true);
  });

  it("returns false when the format parts length does not match date parts", () => {
    expect(isValidDate("2022-01")).toBe(false);
  });

  it("returns false for invalid month value", () => {
    expect(isValidDate("2022-13-01")).toBe(false);
    expect(isValidDate("2022-00-01")).toBe(false);
  });

  it("returns false for invalid day value", () => {
    expect(isValidDate("2022-01-32")).toBe(false);
    expect(isValidDate("2022-01-00")).toBe(false);
  });

  it("validates February days correctly in leap year", () => {
    expect(isValidDate("2020-02-29")).toBe(true);
    expect(isValidDate("2020-02-30")).toBe(false);
  });

  it("validates February days correctly in non-leap year", () => {
    expect(isValidDate("2021-02-28")).toBe(true);
    expect(isValidDate("2021-02-29")).toBe(false);
  });

  it("validates different months with different days", () => {
    expect(isValidDate("2022-04-30")).toBe(true);
    expect(isValidDate("2022-04-31")).toBe(false);
    expect(isValidDate("2022-05-31")).toBe(true);
  });

  it("handles custom format dd/mm/yyyy", () => {
    expect(isValidDate("25/01/2022", "dd/mm/yyyy")).toBe(true);
    expect(isValidDate("32/01/2022", "dd/mm/yyyy")).toBe(false);
  });

  it("handles custom format mm/dd/yyyy", () => {
    expect(isValidDate("01/25/2022", "mm/dd/yyyy")).toBe(true);
    expect(isValidDate("13/25/2022", "mm/dd/yyyy")).toBe(false);
  });
});
