/*
 Copyright (C) 2026 3NSoft Inc.

 This program is free software: you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
 Foundation, either version 3 of the License, or (at your option) any later
 version.

 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 this program. If not, see <http://www.gnu.org/licenses/>.
*/

import type { PlatformResources } from '../platform/inject-defs/platform';
import{ appLog, logError, logWarning, recordUnhandledRejectionsInProcess } from './confs';
import { makeAutoStartupCAP } from './init-proc/auto-startup';
import { AppDownloader } from './caps/system/apps-downloader';
import { SystemPlaces } from './caps/system/system-places';
import { makeAllFileDialogOpeners } from './caps/shell/file-dialogs';
import { makeClipboardCAP } from './caps/shell/clipboard';
import { makeOpenFileCAP, makeOpenFolderCAP, makeOpenURLCAP, openInMountedFolderCAP } from "./caps/shell/openers";
import { makeMountsCAP } from './caps/shell/mounts';
import { Notifications } from './caps/shell/user-notifications';
import { makeUICap } from './caps/ui';
import { makeMediaDevicesCAP } from './caps/media-devices/mediaDevices';
import { makeConnectivity } from './caps/connectivity';
import { LAUNCHER_APP_DOMAIN } from './bundle-confs';
import { dialog } from 'electron';
import { UserLogin } from './caps/system/user-login';
import { makeNativeCryptor } from "napi-nacl";
import { makeServiceLocator, DnsResolver } from 'core-3nweb-client-lib/build/lib-client/service-locator';
import { dohAt } from 'core-3nweb-client-lib';
import { promises as dns } from 'dns';
import { makeNetClient } from 'core-3nweb-client-lib';

export const dnsResolvers: DnsResolver[] = [
	{ resolveTxt: dns.resolveTxt },
	dohAt(`https://cloudflare-dns.com/dns-query`),
	dohAt(`https://dns.google/resolve`)
]

export function makePlatformResources(): PlatformResources {
	return {
		caps: {
			makeAutoStartupCAP,
			makeAppDownloader: AppDownloader.make,
			makeSystemPlaces: SystemPlaces.make,
			makeUICap,
			makeMediaDevicesCAP,
			makeConnectivity,
			shell: {
				makeAllFileDialogOpeners,
				makeClipboardCAP,
				makeOpenFileCAP,
				makeOpenFolderCAP,
				makeOpenURLCAP,
				openInMountedFolderCAP,
				makeMountsCAP,
				makeNotifications: Notifications.make,
			},
		},
		logging: { appLog, logError, logWarning, recordUnhandledRejectionsInProcess },
		LAUNCHER_APP_DOMAIN,
		makeCryptor: makeNativeCryptor,
		makeNetClient,
		makeServiceLocator: makeServiceLocator(...dnsResolvers),
		makeUserLogin: UserLogin.make,
		showSystemErrorBox: dialog.showErrorBox.bind(dialog)
	};
}
