"use strict";
/**
 * 钉住的常用查询条件本地存储工具
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPinnedCommonQueries = getPinnedCommonQueries;
exports.savePinnedCommonQueries = savePinnedCommonQueries;
exports.addPinnedCommonQuery = addPinnedCommonQuery;
exports.removePinnedCommonQuery = removePinnedCommonQuery;
const STORAGE_KEY_PREFIX = 'pinned_common_queries_';
/**
 * 获取存储 key（基于 operator 和 code）
 */
function getStorageKey(operator, code) {
    return `${STORAGE_KEY_PREFIX}${operator}_${code}`;
}
/**
 * 获取所有钉住的常用查询条件
 */
function getPinnedCommonQueries(operator, code) {
    try {
        const key = getStorageKey(operator, code);
        const data = localStorage.getItem(key);
        if (!data)
            return [];
        return JSON.parse(data);
    }
    catch (error) {
        console.error('Failed to get pinned common queries:', error);
        return [];
    }
}
/**
 * 保存钉住的常用查询条件
 */
function savePinnedCommonQueries(operator, code, queries) {
    try {
        const key = getStorageKey(operator, code);
        localStorage.setItem(key, JSON.stringify(queries));
    }
    catch (error) {
        console.error('Failed to save pinned common queries:', error);
    }
}
/**
 * 添加钉住的常用查询条件
 */
function addPinnedCommonQuery(operator, code, translator) {
    var _a;
    const pinnedQueries = getPinnedCommonQueries(operator, code);
    // 检查是否已经钉住过相同的 translator（通过 translatorId 判断）
    const translatorId = (_a = translator.id) !== null && _a !== void 0 ? _a : '';
    const alreadyPinned = pinnedQueries.some((q) => q.translatorId === translatorId);
    // 如果已经钉住过，不重复添加
    if (alreadyPinned) {
        const existing = pinnedQueries.find((q) => q.translatorId === translatorId);
        return existing;
    }
    const newQuery = {
        id: `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
        translatorId,
        tokens: translator.tokens,
        pinnedAt: Date.now(),
    };
    const updatedQueries = [...pinnedQueries, newQuery];
    savePinnedCommonQueries(operator, code, updatedQueries);
    return newQuery;
}
/**
 * 移除钉住的常用查询条件
 */
function removePinnedCommonQuery(operator, code, queryId) {
    const pinnedQueries = getPinnedCommonQueries(operator, code);
    const updatedQueries = pinnedQueries.filter((q) => q.id !== queryId);
    savePinnedCommonQueries(operator, code, updatedQueries);
}
