"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertParamsToBody = exports.getDefaultToSymbolType = exports.isSymbolType = exports.validateToSymbolType = exports.validateFromSymbolType = exports.validateParams = exports.prepareFilter = exports.getNormalizedParams = exports.RCSAssetCategoryGenealogy = exports.SearchAllCategory = exports.method = exports.FROM_SYMBOL_TYPES_DEFAULT_VALUE = void 0;
const common_1 = require("@refinitiv-data/common");
const util_1 = require("../../../util");
const Search = __importStar(require("../search"));
const interfaces_1 = require("./interfaces");
exports.FROM_SYMBOL_TYPES_DEFAULT_VALUE = '_AllUnique';
exports.method = common_1.HttpMethod.POST;
exports.SearchAllCategory = {
    [interfaces_1.AssetClass.Commodities]: 'Commodities',
    [interfaces_1.AssetClass.EquityOrIndexOptions]: 'Options',
    [interfaces_1.AssetClass.BondAndSTIRFuturesAndOptions]: 'Exchange-Traded Rates',
    [interfaces_1.AssetClass.Equities]: 'Equities',
    [interfaces_1.AssetClass.EquityIndexFutures]: 'Futures',
    [interfaces_1.AssetClass.Funds]: 'Funds',
    [interfaces_1.AssetClass.Bonds]: 'Bond Pricing',
    [interfaces_1.AssetClass.FXAndMoney]: 'FX & Money',
};
const isSupportedSearchAllCategory = (asset) => {
    return Object.keys(exports.SearchAllCategory).includes(asset);
};
const formatSearchItem = (item) => "'" + item + "'";
exports.RCSAssetCategoryGenealogy = {
    [interfaces_1.AssetClass.Warrants]: 'A:AA',
    [interfaces_1.AssetClass.Certificates]: 'A:6N',
    [interfaces_1.AssetClass.Indices]: 'I:17',
    [interfaces_1.AssetClass.ReverseConvertible]: 'A:LE',
    [interfaces_1.AssetClass.MiniFuture]: 'A:P6',
};
const isSupportedRCSAssetCategoryGenealogy = (asset) => {
    return Object.keys(exports.RCSAssetCategoryGenealogy).includes(asset);
};
const getNormalizedParams = (params) => {
    const { fromSymbolType, toSymbolType, symbols } = params, restParams = __rest(params, ["fromSymbolType", "toSymbolType", "symbols"]);
    const toSymbol = !toSymbolType ? (0, exports.getDefaultToSymbolType)() : toSymbolType;
    const normalizedToSymbol = Array.isArray(toSymbol)
        ? [...toSymbol, interfaces_1.SymbolType.Description]
        : [toSymbol, interfaces_1.SymbolType.Description];
    return Object.assign({ symbols: Array.isArray(symbols) ? [...symbols] : symbols.split(','), fromSymbolType, toSymbolType: Array.from(new Set(normalizedToSymbol)) }, restParams);
};
exports.getNormalizedParams = getNormalizedParams;
const prepareFilter = (assetState = interfaces_1.AssetState.Active, assetClass = []) => {
    const assetStateString = assetState === interfaces_1.AssetState.Active ? "AssetState eq 'AC'" : "(AssetState ne 'AC' and AssetState ne null)";
    const assetClasses = Array.isArray(assetClass) ? [...assetClass] : [assetClass];
    const appliedSearchAllCategory = assetClasses.filter(isSupportedSearchAllCategory);
    const appliedRCSAssetCategoryGenealogy = assetClasses.filter(isSupportedRCSAssetCategoryGenealogy);
    if (!appliedSearchAllCategory.length && !appliedRCSAssetCategoryGenealogy.length) {
        return `${assetStateString}`;
    }
    const searchAllCategoryString = appliedSearchAllCategory.length
        ? `SearchAllCategoryv3 in (${appliedSearchAllCategory.map(item => formatSearchItem(exports.SearchAllCategory[item])).join(' ')})`
        : '';
    const rcsAssetCategoryGenealogyString = appliedRCSAssetCategoryGenealogy.length
        ? `RCSAssetCategoryGenealogy in (${appliedRCSAssetCategoryGenealogy
            .map(item => formatSearchItem(exports.RCSAssetCategoryGenealogy[item]))
            .join(' ')})`
        : '';
    const orString = searchAllCategoryString && rcsAssetCategoryGenealogyString ? ' or ' : '';
    return `${assetStateString} and (${searchAllCategoryString}${orString}${rcsAssetCategoryGenealogyString})`;
};
exports.prepareFilter = prepareFilter;
const validateParams = (params) => {
    (0, util_1.validateRequired)(params, ['symbols'], 'SymbolConversion.Params');
    const { fromSymbolType, toSymbolType } = params;
    (0, exports.validateFromSymbolType)(fromSymbolType);
    if (toSymbolType) {
        (0, exports.validateToSymbolType)(toSymbolType);
    }
};
exports.validateParams = validateParams;
const validateFromSymbolType = (fromSymbolType) => {
    if (fromSymbolType === undefined || (0, exports.isSymbolType)(fromSymbolType)) {
        return;
    }
    throw new Error('Parameter "fromSymbolType" is invalid.');
};
exports.validateFromSymbolType = validateFromSymbolType;
const validateToSymbolType = (toSymbolType) => {
    const symbolType = Array.isArray(toSymbolType) ? [...toSymbolType] : [toSymbolType];
    const res = symbolType.every(exports.isSymbolType);
    if (!res) {
        throw new Error('Parameter "toSymbolType" is invalid.');
    }
};
exports.validateToSymbolType = validateToSymbolType;
const isSymbolType = (symbolType) => {
    return Object.values(interfaces_1.SymbolType).includes(symbolType);
};
exports.isSymbolType = isSymbolType;
const getDefaultToSymbolType = () => {
    return Object.values(interfaces_1.SymbolType);
};
exports.getDefaultToSymbolType = getDefaultToSymbolType;
const convertParamsToBody = (params) => {
    const { symbols, toSymbolType, fromSymbolType, preferredCountryCode, assetState, assetClass, extendedParams } = params;
    const filter = (0, exports.prepareFilter)(assetState, assetClass);
    return Object.assign(Object.assign(Object.assign({ View: fromSymbolType === interfaces_1.SymbolType.Ticker ? Search.View.QuotesAndSTIRs : Search.View.SearchAll, Scope: fromSymbolType || exports.FROM_SYMBOL_TYPES_DEFAULT_VALUE, Terms: symbols.join(','), Select: toSymbolType.join(',') }, (preferredCountryCode && { Boost: `RCSExchangeCountry eq '${preferredCountryCode}'` })), (filter && { Filter: filter })), extendedParams);
};
exports.convertParamsToBody = convertParamsToBody;
//# sourceMappingURL=data:application/json;base64, 