import { salespersonApi } from '@/api/businessApi.js';
import { getPlatformAdminContext } from '@/subPackages/sub_business_shared/common/context.js';

const BUSINESS_PACKAGE_ROOT = '/subPackages/sub_business_shared';
const SALESPERSON_HOME = '/subPackages/sub_salesperson/home/index';
const SALESPERSON_MY = '/subPackages/sub_salesperson/my/index';

export function getSalespersonContext() {
	return {
		mode: 'salesperson',
		api: salespersonApi,
		userStorageKey: 'salesperson_user',
		homePath: SALESPERSON_HOME,
		myPath: SALESPERSON_MY,
		packageRoot: '/subPackages/sub_salesperson',
		businessPackageRoot: BUSINESS_PACKAGE_ROOT,
		brandTitle: '业务工作台',
		capabilities: {
			canShip: false,
			canRefund: false,
			canStoreFilter: true,
			canStoreInput: true,
			canDoctorInput: true,
		},
		tabList: [
			{ text: '工作台', iconPath: 'home', selectedIconPath: 'home-fill', pagePath: SALESPERSON_HOME },
			{ text: '我的', iconPath: 'account', selectedIconPath: 'account-fill', pagePath: SALESPERSON_MY },
		],
	};
}

/** 录入列表/详情：超管与业务员共用页面时按 loginMode 选 API */
export function getInputBusinessContext() {
	const mode = uni.getStorageSync('loginMode');
	if (mode === 'platform_admin') return getPlatformAdminContext();
	return getSalespersonContext();
}

export function resolveBusinessContext(injected) {
	if (injected && injected.api) return injected;
	return getInputBusinessContext();
}

export function businessPageRoot(ctx) {
	return ctx.businessPackageRoot || ctx.packageRoot;
}

export function pageUrl(ctx, subPath) {
	return `${businessPageRoot(ctx)}/${subPath}`;
}
