/*!
 * Crafted with ❤ by Salla
 */
const INTENT_KEY = 'bullet_delivery_intent';
function getIntentStorage() {
    const rememberLastSession = Boolean(salla.config.get('store.settings.bullet_delivery.settings.remember_last_session'));
    return rememberLastSession ? salla.storage.store : salla.storage.session;
}
export function getStoredIntent() {
    const raw = getIntentStorage().get(INTENT_KEY);
    return raw && typeof raw === 'object' ? raw : null;
}
/** Resolve the stored intent into a city or branch selection. Returns null when no usable intent. */
export function resolveIntent() {
    const intent = getStoredIntent();
    if (!intent)
        return null;
    if (intent.type === 'address') {
        const city = intent.address_details?.city;
        const cityId = city?.id != null ? Number(city.id) : intent.city_id != null ? Number(intent.city_id) : null;
        const cityName = city?.name?.trim() || '';
        if (cityId != null) {
            return { type: 'address', option: { id: cityId, name: cityName } };
        }
        return null;
    }
    if (intent.type === 'branch') {
        const branchId = intent.branch_id != null
            ? Number(intent.branch_id)
            : intent.branch_details?.id != null
                ? Number(intent.branch_details.id)
                : null;
        const branchName = intent.branch_details?.name?.trim() || '';
        if (branchId != null) {
            return { type: 'branch', option: { id: branchId, name: branchName } };
        }
        return null;
    }
    return null;
}
/** Build the payload for opening the bullet delivery modal with preselection. */
export function buildBulletOpenPayload() {
    const intent = getStoredIntent();
    const payload = {};
    if (intent?.type === 'address' && intent.address_id != null) {
        payload.preselected_address_id = Number(intent.address_id);
    }
    if (intent?.type === 'branch') {
        const branchId = intent.branch_id ?? intent.branch_details?.id;
        if (branchId != null) {
            payload.preselected_branch_id = Number(branchId);
        }
    }
    return payload;
}
