// import type { AuthorizationServer } from "oauth4webapi";
// import type { AccountInfo, OAuth2Provider, ProviderOptions } from "../types";

// const authorization_server: AuthorizationServer = {
//   issuer: "https://appleid.apple.com",
//   authorization_endpoint: "https://appleid.apple.com/auth/authorize",
//   token_endpoint: "https://appleid.apple.com/auth/token",
// };

// export function AppleAuthProvider(config: ProviderOptions): OAuth2Provider {
//   return {
//     ...config,
//     id: "apple",
//     scope: "name email",
//     authorization_server,
//     name: "Apple",
//     protocol: "OAuth2",
//     params: {
//       ...config.params,
//       response_mode: "form_post",
//       response_type: "code",
//       // response_mode: "query",
//     },
//     profileCallback: (profile): AccountInfo => {
//       return {
//         sub: profile.id as string,
//         name: profile.name as string,
//         email: profile.email as string,
//         picture: profile.picture as string,
//       };
//     },
//   };
// }

// export default AppleAuthProvider;

import type { AccountInfo, OIDCProvider, ProviderOptions } from "../types";
const issuer = new URL("https://appleid.apple.com");

export function AppleAuthProvider(options: ProviderOptions): OIDCProvider {
  return {
    ...options,
    issuer,
    protocol: "OIDC",
    id: "apple",
    name: "Apple",
    scope: "name email",
    params: {
      ...options.params,
      response_mode: "form_post",
    },
    profileCallback: (profile): AccountInfo => {
      return {
        sub: profile.sub as string,
        name: profile.name as string,
        email: profile.email as string,
        picture: profile.picture as string,
      };
    },
  };
}
