/**
 * 章节适用车型绑定：空列表或显式「全部」表示全部品牌型号适用。
 */

export var VEHICLE_BINDING_ALL_BRAND = '__all__';
export var VEHICLE_BINDING_ALL_MODEL = '__all__';

export function vehicleBindingKey(binding) {
	return String(binding.brand || '') + '|' + String(binding.model || '');
}

export function parseVehicleBindingKey(key) {
	var parts = String(key || '').split('|');
	return { brand: parts[0] || '', model: parts[1] || '' };
}

export function isAllVehicleBindings(bindings) {
	var list = bindings || [];
	if (!list.length) return true;
	if (list.length !== 1) return false;
	var item = list[0] || {};
	return item.brand === VEHICLE_BINDING_ALL_BRAND && item.model === VEHICLE_BINDING_ALL_MODEL;
}

export function isSpecificVehicleBindings(bindings) {
	return !isAllVehicleBindings(bindings);
}

export function normalizeAllVehicleBindings() {
	return [];
}

export function vehicleMatchesBindings(brand, model, bindings) {
	if (isAllVehicleBindings(bindings)) return true;
	var list = bindings || [];
	return list.some(function (item) {
		return item.brand === brand && item.model === model;
	});
}

/** 订单车辆 keys 是否命中章节绑定（任意一辆命中即可；未选车辆时仅「全部」章节可见） */
export function sectionBindingsMatchOrder(bindings, vehicleKeys) {
	if (isAllVehicleBindings(bindings)) return true;
	var keys = vehicleKeys || [];
	if (!keys.length) return false;
	return keys.some(function (key) {
		var pair = parseVehicleBindingKey(key);
		return vehicleMatchesBindings(pair.brand, pair.model, bindings);
	});
}

export function formatVehicleBindings(bindings) {
	if (isAllVehicleBindings(bindings)) return '全部车型';
	var list = bindings || [];
	return list.map(function (item) {
		return item.brand + ' · ' + item.model;
	}).join('；');
}
