/**
 * 对账请求参数（对齐 PC finance/reconciliation/index.vue）
 */

export const STORE_LIST_PAGE_SIZE = 9999

/** Laravel GET 数组：search_time[0] / search_time[1]（uni.request 勿传 JS 数组，否则会变成逗号字符串） */
export function buildSearchTimeQueryParams(dateStart, dateEnd) {
	return {
		'search_time[0]': dateStart,
		'search_time[1]': dateEnd,
	}
}

export function buildStoreListParams({
	page = 1,
	pageSize = STORE_LIST_PAGE_SIZE,
	dateStart,
	dateEnd,
	storeName = '',
	excludeZeroSales = 0,
}) {
	const params = {
		page,
		pageSize,
		exclude_zero_sales: excludeZeroSales,
		...buildSearchTimeQueryParams(dateStart, dateEnd),
	}
	if (storeName) params.store_name = storeName
	return params
}

export function buildDrugListParams({
	dateStart,
	dateEnd,
	drugId,
	orderId,
	byOrder = false,
	storeId,
}) {
	const params = {
		...buildSearchTimeQueryParams(dateStart, dateEnd),
	}
	if (drugId) params.drug_id = drugId
	if (orderId) params.order_id = orderId
	if (byOrder) params.by_order = 1
	if (storeId != null) params.store_id = storeId
	return params
}

export function normalizeReconciliationResponse(data) {
	const d = data || {}
	const listWrap = d.list || {}
	const raw = listWrap.items
	return {
		count: d.count || {},
		items: Array.isArray(raw) ? raw : [],
		pageMeta: listWrap,
	}
}

/** 本月 1 日 YYYY-MM-DD */
export function defaultMonthStartDate() {
	const d = new Date()
	return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-01`
}

export function defaultTodayDate() {
	return new Date().toISOString().slice(0, 10)
}
