"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_AUTOMATION_TYPES = exports.AUTOMATION_BEHAVIOR_TYPES = void 0;
exports.normalizeLegacyAutomationBehaviorType = normalizeLegacyAutomationBehaviorType;
exports.sanitizeAutomationContexts = sanitizeAutomationContexts;
exports.sanitizeAutomationTypes = sanitizeAutomationTypes;
exports.getAutomationTypesConfig = getAutomationTypesConfig;
exports.upsertAutomationTypesConfig = upsertAutomationTypesConfig;
exports.resolveAutomationType = resolveAutomationType;
exports.resolveStoredAutomationTypeId = resolveStoredAutomationTypeId;
exports.normalizeAutomationContextSelection = normalizeAutomationContextSelection;
exports.resolveAutomationTypeContexts = resolveAutomationTypeContexts;
var client_js_1 = require("../db/client.js");
var app_config_service_js_1 = require("./app-config-service.js");
var AUTOMATION_TYPES_TABLE = 'automation_types';
var AUTOMATION_TYPES_CONFIG_KEY = 'automationTypes';
exports.AUTOMATION_BEHAVIOR_TYPES = [
    'none',
    'plan',
    'command_execution',
    'non_source_work',
    'auto_worker',
];
exports.DEFAULT_AUTOMATION_TYPES = [
    {
        id: 'general-inquiry',
        name: '일반 문의',
        description: '일반 문의/확인 요청으로 처리합니다.',
        contexts: [
            {
                id: 'general-inquiry-default',
                title: '기본 확인',
                content: '소스 수정 없이 조회, 상태 확인, 응답 정리를 우선합니다.',
                enabled: true,
                defaultSelected: true,
                updatedAt: '2026-04-29T00:00:00.000Z',
            },
        ],
        behaviorType: 'command_execution',
        enabled: true,
        updatedAt: '2026-04-29T00:00:00.000Z',
    },
    {
        id: 'none',
        name: '기본유형',
        description: '기본 자동화 처리용 유형입니다.',
        contexts: [
            {
                id: 'none-default',
                title: '기본 처리',
                content: '## 기본 처리\n- 현재 저장소 규칙과 요청 본문을 우선 따릅니다.\n- 소스 수정이 필요하면 관련 파일을 수정하고, 필요 없으면 확인 결과만 정리합니다.\n- 작업 결과에는 변경 파일, diff, preview 같은 증적을 남길 수 있습니다.',
                enabled: true,
                defaultSelected: true,
                updatedAt: '2026-04-29T00:00:00.000Z',
            },
        ],
        behaviorType: 'none',
        enabled: true,
        updatedAt: '2026-04-23T00:00:00.000Z',
    },
    {
        id: 'plan',
        name: '작업 요청 등록',
        description: 'Plan 등록/문서형 자동화 요청으로 처리합니다.',
        contexts: [
            {
                id: 'plan-default',
                title: '문서형 처리',
                content: 'Plan 등록/문서형 요청으로 처리하고, 구현보다 등록/정리 결과를 우선 남깁니다.',
                enabled: true,
                defaultSelected: true,
                updatedAt: '2026-04-29T00:00:00.000Z',
            },
        ],
        behaviorType: 'plan',
        enabled: true,
        updatedAt: '2026-04-23T00:00:00.000Z',
    },
    {
        id: 'command_execution',
        name: 'Command 실행',
        description: '저장소 수정 없이 명령 실행/조회 중심으로 처리합니다.',
        contexts: [
            {
                id: 'command-execution-default',
                title: '명령 실행',
                content: '저장소 수정 없이 명령 실행, API 확인, 상태 조회 중심으로 처리합니다.',
                enabled: true,
                defaultSelected: true,
                updatedAt: '2026-04-29T00:00:00.000Z',
            },
        ],
        behaviorType: 'command_execution',
        enabled: true,
        updatedAt: '2026-04-23T00:00:00.000Z',
    },
    {
        id: 'non_source_work',
        name: '비 소스작업',
        description: '문서/운영/확인 등 비소스 작업으로 처리합니다.',
        contexts: [
            {
                id: 'non-source-work-default',
                title: '비소스 작업',
                content: '문서, 운영, 데이터 확인 등 비소스 작업으로 처리하고 코드 수정은 요청 시에만 제한적으로 수행합니다.',
                enabled: true,
                defaultSelected: true,
                updatedAt: '2026-04-29T00:00:00.000Z',
            },
        ],
        behaviorType: 'non_source_work',
        enabled: true,
        updatedAt: '2026-04-23T00:00:00.000Z',
    },
    {
        id: 'auto_worker',
        name: 'autoWorker',
        description: '자동화 작업메모로 처리합니다.',
        contexts: [
            {
                id: 'auto-worker-default',
                title: '자동화 기본 규칙',
                content: '## context 사용 규칙\n- 자동화 실행기는 선택된 자동화 유형의 context만 우선 참조합니다.\n- Codex Live 문맥이나 일반 채팅 문맥은 자동화 기본 context로 섞지 않습니다.\n\n## 처리 원칙\n- 세부 절차는 현재 운영 설정을 따릅니다.',
                enabled: true,
                defaultSelected: true,
                updatedAt: '2026-04-29T00:00:00.000Z',
            },
        ],
        behaviorType: 'auto_worker',
        enabled: true,
        updatedAt: '2026-04-23T00:00:00.000Z',
    },
];
function normalizeText(value) {
    return typeof value === 'string' ? value.trim() : '';
}
function normalizeEnabled(value) {
    if (typeof value === 'boolean') {
        return value;
    }
    if (typeof value === 'number') {
        return value !== 0;
    }
    if (typeof value === 'string') {
        var normalizedValue = value.trim().toLowerCase();
        if (['false', '0', 'n', 'no', 'off'].includes(normalizedValue)) {
            return false;
        }
        if (['true', '1', 'y', 'yes', 'on'].includes(normalizedValue)) {
            return true;
        }
    }
    return value !== false;
}
function normalizeLegacyAutomationTypeId(value) {
    var normalizedValue = normalizeLegacyAutomationBehaviorType(value);
    if (normalizedValue === 'stock-alert') {
        return 'general-inquiry';
    }
    return normalizedValue;
}
function normalizeBehaviorType(value) {
    var normalizedValue = normalizeLegacyAutomationBehaviorType(value);
    return exports.AUTOMATION_BEHAVIOR_TYPES.includes(normalizedValue)
        ? normalizedValue
        : 'none';
}
function normalizeLegacyAutomationBehaviorType(value) {
    var normalizedValue = normalizeText(value);
    if (normalizedValue === 'plan_registration') {
        return 'plan';
    }
    if (normalizedValue === 'general_development') {
        return 'auto_worker';
    }
    return normalizedValue;
}
function buildNameKey(value) {
    return value.replace(/\s+/g, ' ').toLocaleLowerCase('ko-KR');
}
function buildContextTitleKey(value) {
    return value.replace(/\s+/g, ' ').toLocaleLowerCase('ko-KR');
}
function normalizeAutomationContext(record) {
    var title = normalizeText(record.title);
    var content = normalizeText(record.content);
    if (!title && !content) {
        return null;
    }
    var rawId = normalizeText(record.id);
    var normalizedId = rawId || "automation-context-".concat(Date.now().toString(36), "-").concat(Math.random().toString(36).slice(2, 8));
    return {
        id: normalizedId,
        title: title || 'Context',
        content: content,
        enabled: normalizeEnabled(record.enabled),
        defaultSelected: normalizeEnabled(record.defaultSelected),
        updatedAt: normalizeText(record.updatedAt) || new Date().toISOString(),
    };
}
function compareContextUpdatedAt(left, right) {
    var leftTime = Date.parse(left.updatedAt);
    var rightTime = Date.parse(right.updatedAt);
    if (Number.isFinite(leftTime) && Number.isFinite(rightTime) && leftTime !== rightTime) {
        return leftTime - rightTime;
    }
    return 0;
}
function sanitizeAutomationContexts(items) {
    var byId = new Map();
    var bySemanticKey = new Map();
    (items !== null && items !== void 0 ? items : [])
        .map(function (item) { return normalizeAutomationContext(item); })
        .filter(function (item) { return Boolean(item); })
        .forEach(function (item) {
        var currentById = byId.get(item.id);
        if (!currentById || compareContextUpdatedAt(currentById, item) <= 0) {
            byId.set(item.id, item);
        }
    });
    for (var _i = 0, _a = byId.values(); _i < _a.length; _i++) {
        var item = _a[_i];
        var semanticKey = buildContextTitleKey(item.title);
        var current = bySemanticKey.get(semanticKey);
        if (!current || compareContextUpdatedAt(current, item) <= 0) {
            bySemanticKey.set(semanticKey, item);
        }
    }
    return Array.from(bySemanticKey.values()).sort(function (left, right) { return left.title.localeCompare(right.title, 'ko-KR'); });
}
function normalizeAutomationType(record) {
    var name = normalizeText(record.name);
    if (!name) {
        return null;
    }
    var rawId = normalizeText(record.id);
    var normalizedId = rawId || "automation-type-".concat(Date.now().toString(36), "-").concat(Math.random().toString(36).slice(2, 8));
    return {
        id: normalizedId,
        name: name,
        description: normalizeText(record.description),
        contexts: sanitizeAutomationContexts(record.contexts),
        behaviorType: normalizeBehaviorType(record.behaviorType),
        enabled: normalizeEnabled(record.enabled),
        updatedAt: normalizeText(record.updatedAt) || new Date().toISOString(),
    };
}
function compareUpdatedAt(left, right) {
    var leftTime = Date.parse(left.updatedAt);
    var rightTime = Date.parse(right.updatedAt);
    if (Number.isFinite(leftTime) && Number.isFinite(rightTime) && leftTime !== rightTime) {
        return leftTime - rightTime;
    }
    return 0;
}
function dedupeAutomationTypes(items) {
    var byId = new Map();
    var bySemanticKey = new Map();
    for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
        var item = items_1[_i];
        var currentById = byId.get(item.id);
        if (!currentById || compareUpdatedAt(currentById, item) <= 0) {
            byId.set(item.id, item);
        }
    }
    for (var _a = 0, _b = byId.values(); _a < _b.length; _a++) {
        var item = _b[_a];
        var semanticKey = "".concat(item.behaviorType, ":").concat(buildNameKey(item.name));
        var current = bySemanticKey.get(semanticKey);
        if (!current || compareUpdatedAt(current, item) <= 0) {
            bySemanticKey.set(semanticKey, item);
        }
    }
    return Array.from(bySemanticKey.values()).sort(function (left, right) { return left.name.localeCompare(right.name, 'ko-KR'); });
}
function sanitizeAutomationTypes(items) {
    var normalized = (items !== null && items !== void 0 ? items : [])
        .map(function (item) { return normalizeAutomationType(item); })
        .filter(function (item) { return Boolean(item); });
    if (normalized.length === 0) {
        return exports.DEFAULT_AUTOMATION_TYPES;
    }
    return dedupeAutomationTypes(normalized);
}
function mergeDefaultAutomationTypes(items) {
    var _a;
    var byId = new Map(items.map(function (item) { return [item.id, item]; }));
    for (var _i = 0, DEFAULT_AUTOMATION_TYPES_1 = exports.DEFAULT_AUTOMATION_TYPES; _i < DEFAULT_AUTOMATION_TYPES_1.length; _i++) {
        var defaultItem = DEFAULT_AUTOMATION_TYPES_1[_i];
        var existingItem = byId.get(defaultItem.id);
        if (!existingItem) {
            byId.set(defaultItem.id, defaultItem);
            continue;
        }
        byId.set(defaultItem.id, __assign(__assign({}, existingItem), { name: defaultItem.name, description: existingItem.description || defaultItem.description, contexts: sanitizeAutomationContexts(((_a = existingItem.contexts) === null || _a === void 0 ? void 0 : _a.length) ? existingItem.contexts : defaultItem.contexts), behaviorType: defaultItem.behaviorType }));
    }
    return sanitizeAutomationTypes(Array.from(byId.values()));
}
function isSameAutomationTypeList(left, right) {
    if (left.length !== right.length) {
        return false;
    }
    return left.every(function (item, index) {
        var target = right[index];
        return (target &&
            item.id === target.id &&
            item.name === target.name &&
            item.description === target.description &&
            JSON.stringify(item.contexts) === JSON.stringify(target.contexts) &&
            item.behaviorType === target.behaviorType &&
            item.enabled === target.enabled &&
            item.updatedAt === target.updatedAt);
    });
}
function ensureAutomationTypesTable() {
    return __awaiter(this, void 0, void 0, function () {
        var hasTable, requiredColumns, _loop_1, _i, requiredColumns_1, _a, columnName, createColumn;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0: return [4 /*yield*/, client_js_1.db.schema.hasTable(AUTOMATION_TYPES_TABLE)];
                case 1:
                    hasTable = _b.sent();
                    if (!!hasTable) return [3 /*break*/, 3];
                    return [4 /*yield*/, client_js_1.db.schema.createTable(AUTOMATION_TYPES_TABLE, function (table) {
                            table.string('id').primary();
                            table.string('name').notNullable();
                            table.text('description').notNullable().defaultTo('');
                            table.text('contexts_json').notNullable().defaultTo('[]');
                            table.string('behavior_type').notNullable();
                            table.boolean('enabled').notNullable().defaultTo(true);
                            table.timestamp('updated_at', { useTz: true }).notNullable().defaultTo(client_js_1.db.fn.now());
                        })];
                case 2:
                    _b.sent();
                    return [2 /*return*/];
                case 3:
                    requiredColumns = [
                        ['name', function (table) { return table.string('name').notNullable().defaultTo(''); }],
                        ['description', function (table) { return table.text('description').notNullable().defaultTo(''); }],
                        ['contexts_json', function (table) { return table.text('contexts_json').notNullable().defaultTo('[]'); }],
                        ['behavior_type', function (table) { return table.string('behavior_type').notNullable().defaultTo('none'); }],
                        ['enabled', function (table) { return table.boolean('enabled').notNullable().defaultTo(true); }],
                        ['updated_at', function (table) { return table.timestamp('updated_at', { useTz: true }).notNullable().defaultTo(client_js_1.db.fn.now()); }],
                    ];
                    _loop_1 = function (columnName, createColumn) {
                        var hasColumn;
                        return __generator(this, function (_c) {
                            switch (_c.label) {
                                case 0: return [4 /*yield*/, client_js_1.db.schema.hasColumn(AUTOMATION_TYPES_TABLE, columnName)];
                                case 1:
                                    hasColumn = _c.sent();
                                    if (!!hasColumn) return [3 /*break*/, 3];
                                    return [4 /*yield*/, client_js_1.db.schema.alterTable(AUTOMATION_TYPES_TABLE, function (table) {
                                            createColumn(table);
                                        })];
                                case 2:
                                    _c.sent();
                                    _c.label = 3;
                                case 3: return [2 /*return*/];
                            }
                        });
                    };
                    _i = 0, requiredColumns_1 = requiredColumns;
                    _b.label = 4;
                case 4:
                    if (!(_i < requiredColumns_1.length)) return [3 /*break*/, 7];
                    _a = requiredColumns_1[_i], columnName = _a[0], createColumn = _a[1];
                    return [5 /*yield**/, _loop_1(columnName, createColumn)];
                case 5:
                    _b.sent();
                    _b.label = 6;
                case 6:
                    _i++;
                    return [3 /*break*/, 4];
                case 7: return [2 /*return*/];
            }
        });
    });
}
function normalizeConfigRecord(value) {
    if (!value || typeof value !== 'object' || Array.isArray(value)) {
        return {};
    }
    return value;
}
function parseContextsFromRow(row) {
    var rawValue = row.contexts_json;
    if (typeof rawValue !== 'string') {
        return [];
    }
    try {
        var parsed = JSON.parse(rawValue);
        return Array.isArray(parsed) ? parsed : [];
    }
    catch (_a) {
        return [];
    }
}
function toAutomationTypeRecord(row) {
    return normalizeAutomationType({
        id: typeof row.id === 'string' ? row.id : undefined,
        name: typeof row.name === 'string' ? row.name : undefined,
        description: typeof row.description === 'string' ? row.description : undefined,
        contexts: parseContextsFromRow(row),
        behaviorType: normalizeLegacyAutomationBehaviorType(row.behavior_type),
        enabled: normalizeEnabled(row.enabled),
        updatedAt: typeof row.updated_at === 'string' ? row.updated_at : undefined,
    });
}
function seedAutomationTypesFromLegacyConfig() {
    return __awaiter(this, void 0, void 0, function () {
        var config, _a, raw, legacyItems;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    _a = normalizeConfigRecord;
                    return [4 /*yield*/, (0, app_config_service_js_1.getAppConfig)()];
                case 1:
                    config = _a.apply(void 0, [_b.sent()]);
                    raw = config[AUTOMATION_TYPES_CONFIG_KEY];
                    if (!Array.isArray(raw) || raw.length === 0) {
                        return [2 /*return*/, mergeDefaultAutomationTypes(exports.DEFAULT_AUTOMATION_TYPES)];
                    }
                    legacyItems = mergeDefaultAutomationTypes(sanitizeAutomationTypes(raw));
                    return [4 /*yield*/, replaceAutomationTypesInTable(legacyItems)];
                case 2:
                    _b.sent();
                    return [2 /*return*/, legacyItems];
            }
        });
    });
}
function readAutomationTypesFromTable() {
    return __awaiter(this, void 0, void 0, function () {
        var rows, savedItems;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, ensureAutomationTypesTable()];
                case 1:
                    _a.sent();
                    return [4 /*yield*/, (0, client_js_1.db)(AUTOMATION_TYPES_TABLE)
                            .select('id', 'name', 'description', 'contexts_json', 'behavior_type', 'enabled', 'updated_at')
                            .orderBy('name', 'asc')];
                case 2:
                    rows = _a.sent();
                    savedItems = rows
                        .map(function (row) { return toAutomationTypeRecord(row); })
                        .filter(function (item) { return Boolean(item); });
                    return [2 /*return*/, sanitizeAutomationTypes(savedItems)];
            }
        });
    });
}
function replaceAutomationTypesInTable(items) {
    return __awaiter(this, void 0, void 0, function () {
        var nextItems;
        var _this = this;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, ensureAutomationTypesTable()];
                case 1:
                    _a.sent();
                    nextItems = sanitizeAutomationTypes(items);
                    return [4 /*yield*/, client_js_1.db.transaction(function (trx) { return __awaiter(_this, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4 /*yield*/, trx(AUTOMATION_TYPES_TABLE).del()];
                                    case 1:
                                        _a.sent();
                                        return [4 /*yield*/, trx(AUTOMATION_TYPES_TABLE).insert(nextItems.map(function (item) {
                                                var _a;
                                                return ({
                                                    id: item.id,
                                                    name: item.name,
                                                    description: item.description,
                                                    contexts_json: JSON.stringify((_a = item.contexts) !== null && _a !== void 0 ? _a : []),
                                                    behavior_type: item.behaviorType,
                                                    enabled: item.enabled,
                                                    updated_at: item.updatedAt,
                                                });
                                            }))];
                                    case 2:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); })];
                case 2:
                    _a.sent();
                    return [2 /*return*/, nextItems];
            }
        });
    });
}
function getAutomationTypesConfig() {
    return __awaiter(this, void 0, void 0, function () {
        var savedAutomationTypes, automationTypes;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, readAutomationTypesFromTable()];
                case 1:
                    savedAutomationTypes = _a.sent();
                    automationTypes = mergeDefaultAutomationTypes(savedAutomationTypes);
                    if (automationTypes.length === 0 || automationTypes === exports.DEFAULT_AUTOMATION_TYPES) {
                        return [2 /*return*/, seedAutomationTypesFromLegacyConfig()];
                    }
                    if (!!isSameAutomationTypeList(savedAutomationTypes, automationTypes)) return [3 /*break*/, 3];
                    return [4 /*yield*/, replaceAutomationTypesInTable(automationTypes)];
                case 2:
                    _a.sent();
                    _a.label = 3;
                case 3: return [2 /*return*/, automationTypes];
            }
        });
    });
}
function upsertAutomationTypesConfig(items) {
    return __awaiter(this, void 0, void 0, function () {
        var nextAutomationTypes;
        return __generator(this, function (_a) {
            nextAutomationTypes = mergeDefaultAutomationTypes(sanitizeAutomationTypes(Array.isArray(items) ? items : []));
            return [2 /*return*/, replaceAutomationTypesInTable(nextAutomationTypes)];
        });
    });
}
function resolveAutomationType(input) {
    return __awaiter(this, void 0, void 0, function () {
        var requestedId, automationTypes, matched, matchedByBehavior;
        var _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    requestedId = normalizeLegacyAutomationTypeId(input);
                    return [4 /*yield*/, getAutomationTypesConfig()];
                case 1:
                    automationTypes = _b.sent();
                    matched = automationTypes.find(function (item) { return item.id === requestedId; });
                    if (matched) {
                        return [2 /*return*/, matched];
                    }
                    matchedByBehavior = automationTypes.find(function (item) { return item.behaviorType === normalizeBehaviorType(requestedId); });
                    if (matchedByBehavior) {
                        return [2 /*return*/, matchedByBehavior];
                    }
                    return [2 /*return*/, ((_a = exports.DEFAULT_AUTOMATION_TYPES.find(function (item) { return item.id === normalizeBehaviorType(requestedId); })) !== null && _a !== void 0 ? _a : exports.DEFAULT_AUTOMATION_TYPES[0])];
            }
        });
    });
}
function resolveStoredAutomationTypeId(row) {
    var automationTypeId = normalizeText(row.automation_type_id);
    if (automationTypeId) {
        return normalizeLegacyAutomationTypeId(automationTypeId);
    }
    return normalizeLegacyAutomationTypeId(row.automation_type) || 'none';
}
function normalizeAutomationContextSelection(value) {
    var rawValues = Array.isArray(value)
        ? value
        : typeof value === 'string'
            ? value
                .split(',')
                .map(function (item) { return item.trim(); })
                .filter(Boolean)
            : [];
    return __spreadArray([], new Set(rawValues.map(function (item) { return normalizeText(String(item)); }).filter(Boolean)), true);
}
function resolveAutomationTypeContexts(automationType, selectedContextIds) {
    var contexts = sanitizeAutomationContexts(automationType === null || automationType === void 0 ? void 0 : automationType.contexts);
    var requestedIds = normalizeAutomationContextSelection(selectedContextIds);
    if (requestedIds.length === 0 && Array.isArray(selectedContextIds)) {
        return [];
    }
    if (requestedIds.length === 0) {
        return contexts.filter(function (item) { return item.enabled && item.defaultSelected; });
    }
    var requestedIdSet = new Set(requestedIds);
    return contexts.filter(function (item) { return requestedIdSet.has(item.id); });
}
