import { createBrowserRouter, RouterProvider } from 'react-router';
import { AttendanceScene } from './app/AttendanceView/AttendanceScene';
import { ErrorScene } from './app/error';
import { HomeScene } from './app/Home/HomeScene';
import { LeaveApplicationScene } from './app/LeaveApplicationRecord/LeaveApplicationScene';
import { AdminLeaveApplicationScene } from './app/LeaveApplicationRecordForAdmin/AdminLeaveApplicationScene';
import { LeaveCancellationScene } from './app/LeaveCancellationRecord/LeaveCancellationScene';
import { LeaveCreditApplicationScene } from './app/LeaveCreditApplicationRecord/LeaveCreditApplicationScene';
import { LeaveTransactionScene } from './app/LeaveTransactionRecord/LeaveTransactionScene';
import { ReportScene } from './app/Report/ReportScene';
import { Root } from './app/Root';
import { StaffScene } from './app/StaffRecord/StaffScene';
import { Route } from './constant';
import Login from './login';

const router = createBrowserRouter([
  {
    path: '/',
    Component: Root,
    errorElement: <ErrorScene />,
    // loader: async ({ request }) => {
    // const url = new URL(request.url);
    // const store = getDefaultStore();
    // const token = store.get(tokenAtom);
    // if (!token && url.pathname !== Route.Login) {
    //   return redirect(`${Route.Login}?redirect=${url.pathname + url.search}`);
    // }
    // if (token && url.pathname === Route.Login) {
    //   const redirectUrl = url.searchParams.get('redirect');
    //   return redirectUrl ? redirect(redirectUrl) : redirect(Route.Root);
    // }
    // if (url.pathname === '/') return redirect(Route.Root);
    // return true;
    // },
    children: [
      { path: Route.Login, Component: Login },
      { index: true, Component: HomeScene },

      { path: 'staff-record', Component: StaffScene },
      { path: 'leave-application-record', Component: LeaveApplicationScene },
      { path: 'leave-cancellation-record', Component: LeaveCancellationScene },
      {
        path: 'leave-application-record-admin',
        Component: AdminLeaveApplicationScene,
      },
      {
        path: 'leave-transaction-record',
        Component: LeaveTransactionScene,
      },
      {
        path: 'leave-credit-application-record',
        Component: LeaveCreditApplicationScene,
      },
      {
        path: 'attendance-record',
        Component: AttendanceScene,
      },
      {
        path: 'report-record',
        Component: ReportScene,
      },
    ],
  },
  // { path: '/editor/:jobId', Component: EditorScene },
]);

export default function AppRouter() {
  return <RouterProvider router={router} />;
}
