"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = __toESM(require("react"));
var import_react2 = require("@testing-library/react");
var import_date_fns = require("date-fns");
var import__ = require("../..");
var import_Datepicker = require("./Datepicker");
describe("<Datepicker />", () => {
  const testDate = new Date(2022, 7, 3, 14, 0, 0);
  let originalDateTimeFormat;
  beforeAll(() => {
    originalDateTimeFormat = Intl.DateTimeFormat;
  });
  describe("Snapshots", () => {
    it("should match snapshot when v3Theme is settled", () => {
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { theme: import__.v3theme, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single" }) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with single Datepicker", () => {
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single" }) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with range Datepicker", () => {
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "range" }) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with default selected date single Datepicker", () => {
      jest.useFakeTimers("modern").setSystemTime(testDate);
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", startDate: testDate }) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with default selected dates range Datepicker", () => {
      jest.useFakeTimers("modern").setSystemTime(testDate);
      const endDate = /* @__PURE__ */ new Date();
      endDate.setDate(testDate.getDate() + 15);
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "range", startDate: testDate, endDate }) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with custom placeholder single Datepicker", () => {
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import__.Datepicker,
          {
            type: "single",
            placeholder: "This is a custom placeholder :)"
          }
        ) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with disabled past dates Datepicker", () => {
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", disablePastDates: true }) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with disabled future dates Datepicker", () => {
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", disableFutureDates: true }) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with disabled Datepicker", () => {
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", disabled: true }) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with error Datepicker", () => {
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", error: "This is an error" }) })
      );
      expect(container).toMatchSnapshot();
    });
  });
  describe("Unit", () => {
    const dateFormat = "MMM d, yyyy";
    it("should call onSelectSingle function on single Datepicker", () => {
      const onSelectSingle = jest.fn();
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", onSelectSingle }) })
      );
      expect(onSelectSingle).not.toHaveBeenCalled();
      import_react2.fireEvent.click(import_react2.screen.getByRole("button"));
      const day10 = import_react2.screen.queryAllByText("10")[0];
      import_react2.fireEvent.click(day10);
      expect(onSelectSingle).toHaveBeenCalledTimes(1);
    });
    it("should call onSelectRange function on range Datepicker", () => {
      const onSelectRange = jest.fn();
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "range", onSelectRange }) })
      );
      expect(onSelectRange).not.toHaveBeenCalled();
      import_react2.fireEvent.click(import_react2.screen.getByRole("button"));
      const day10 = import_react2.screen.queryAllByText("10")[0];
      const day20 = import_react2.screen.queryAllByText("20")[0];
      import_react2.fireEvent.click(day10);
      expect(onSelectRange).toHaveBeenCalledTimes(1);
      import_react2.fireEvent.click(day20);
      expect(onSelectRange).toHaveBeenCalledTimes(2);
    });
    it("should call onCustomSelectRange function on range Datepicker", () => {
      const onCustomSelectRange = jest.fn();
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "range", onSelectRange: onCustomSelectRange }) })
      );
      expect(onCustomSelectRange).not.toHaveBeenCalled();
      import_react2.fireEvent.click(import_react2.screen.getByRole("button"));
      const day10 = import_react2.screen.queryAllByText("10")[0];
      const day20 = import_react2.screen.queryAllByText("20")[0];
      import_react2.fireEvent.click(day10);
      expect(onCustomSelectRange).toHaveBeenCalledTimes(1);
      import_react2.fireEvent.click(day20);
      expect(onCustomSelectRange).toHaveBeenCalledTimes(2);
    });
    it("should display default date on single Datepicker", () => {
      const currentDate = /* @__PURE__ */ new Date();
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", startDate: currentDate }) })
      );
      const formattedDate = (0, import_date_fns.format)((0, import_Datepicker.toUTC)(currentDate), dateFormat);
      expect(import_react2.screen.getByText(formattedDate)).toBeVisible();
    });
    it("should display default date range on range Datepicker", () => {
      const currentDate = /* @__PURE__ */ new Date();
      const end = /* @__PURE__ */ new Date();
      end.setDate(currentDate.getDate() + 15);
      const formattedStartDate = (0, import_date_fns.format)((0, import_Datepicker.toUTC)(currentDate), dateFormat);
      const formattedEndDate = (0, import_date_fns.format)((0, import_Datepicker.toUTC)(end), dateFormat);
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "range", startDate: currentDate, endDate: end }) })
      );
      expect(
        import_react2.screen.getByText(`${formattedStartDate} - ${formattedEndDate}`)
      ).toBeVisible();
    });
    it('should display custom placeholder and "Select Date" if none is provided', () => {
      const { rerender } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import__.Datepicker,
          {
            type: "single",
            placeholder: "This is a custom placeholder :)"
          }
        ) })
      );
      expect(import_react2.screen.getByText("This is a custom placeholder :)")).toBeVisible();
      rerender(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single" }) })
      );
      expect(import_react2.screen.getByText("Select Date")).toBeVisible();
    });
    it("should disable past dates", () => {
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", disablePastDates: true }) })
      );
      import_react2.fireEvent.click(import_react2.screen.getByRole("button"));
      const previousMonthArrow = import_react2.screen.getByTestId("previous-month-arrow");
      import_react2.fireEvent.click(previousMonthArrow);
      [...Array(25)].forEach(
        (_e, i) => expect(
          import_react2.screen.getAllByText(`${i + 1}`)[0].closest("div")
        ).toHaveAttribute("disabled")
      );
    });
    it("should disable future dates", () => {
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", startDate: testDate, disableFutureDates: true }) })
      );
      import_react2.fireEvent.click(import_react2.screen.getByRole("button"));
      const nextMonthArrow = import_react2.screen.getByTestId("next-month-arrow");
      import_react2.fireEvent.click(nextMonthArrow);
      [...Array(25)].forEach(
        (_e, i) => expect(
          import_react2.screen.getAllByText(`${i + 1}`)[0].closest("div")
        ).toHaveAttribute("disabled")
      );
    });
    it("should disable past dates from specific date", () => {
      jest.useFakeTimers("modern").setSystemTime(testDate);
      const date = /* @__PURE__ */ new Date();
      date.setDate(testDate.getDate() + 15);
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", disablePastFrom: date }) })
      );
      import_react2.fireEvent.click(import_react2.screen.getByRole("button"));
      for (let i = 1; i <= 17; i++) {
        expect(import_react2.screen.getAllByText(`${i}`)[0].closest("div")).toHaveAttribute(
          "disabled"
        );
      }
    });
    it("should disable future dates from specific date", () => {
      jest.useFakeTimers("modern").setSystemTime(testDate);
      const date = /* @__PURE__ */ new Date();
      date.setDate(testDate.getDate() + 15);
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", disableFutureFrom: date }) })
      );
      import_react2.fireEvent.click(import_react2.screen.getByRole("button"));
      for (let i = 19; i <= 31; i++) {
        expect(import_react2.screen.getAllByText(`${i}`)[0].closest("div")).toHaveAttribute(
          "disabled"
        );
      }
    });
    it("should be disabled", () => {
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", startDate: testDate, disabled: true }) })
      );
      const formattedDate = (0, import_date_fns.format)((0, import_Datepicker.toUTC)(testDate), dateFormat);
      const datepickerInput = import_react2.screen.getByText(formattedDate);
      expect(datepickerInput).toHaveAttribute("disabled");
      expect(datepickerInput.closest("div")).toHaveAttribute("disabled");
    });
    it("should display an error", () => {
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", error: "This is an error" }) })
      );
      expect(import_react2.screen.getByText("This is an error")).toBeVisible();
    });
  });
  describe("Internationalization", () => {
    let originalNavigator;
    beforeEach(() => {
      originalNavigator = global.navigator;
    });
    afterEach(() => {
      global.Intl.DateTimeFormat = originalDateTimeFormat;
      Object.defineProperty(global, "navigator", {
        value: originalNavigator,
        writable: true
      });
    });
    it("should display calendar in English when locale is not supported", () => {
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", locale: "xx-XX" }) })
      );
      import_react2.fireEvent.click(import_react2.screen.getByRole("button"));
      const currentDate = /* @__PURE__ */ new Date();
      const currentMonth = new Intl.DateTimeFormat("en-US", {
        month: "long"
      }).format(currentDate);
      expect(
        import_react2.screen.getByText(new RegExp(currentMonth, "i"))
      ).toBeInTheDocument();
    });
    it("should display calendar in Portuguese when browser locale is pt-BR", () => {
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single", locale: "pt-BR" }) })
      );
      import_react2.fireEvent.click(import_react2.screen.getByRole("button"));
      const currentDate = /* @__PURE__ */ new Date();
      const months = [
        "janeiro",
        "fevereiro",
        "mar\xE7o",
        "abril",
        "maio",
        "junho",
        "julho",
        "agosto",
        "setembro",
        "outubro",
        "novembro",
        "dezembro"
      ];
      const currentMonth = months[currentDate.getMonth()];
      expect(
        import_react2.screen.getByText(new RegExp(currentMonth, "i"))
      ).toBeInTheDocument();
    });
    it("should display only the first letter of each weekday in uppercase", () => {
      (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Datepicker, { type: "single" }) })
      );
      import_react2.fireEvent.click(import_react2.screen.getByRole("button"));
      const weekdayLetters = ["S", "M", "T", "W", "T", "F", "S"];
      weekdayLetters.forEach((letter) => {
        expect(import_react2.screen.getAllByText(letter).length).toBeGreaterThan(0);
      });
      expect(import_react2.screen.queryByText("sun")).not.toBeInTheDocument();
      expect(import_react2.screen.queryByText("mon")).not.toBeInTheDocument();
    });
  });
});
