"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var forEach = require("lodash/forEach");
var isArray = require("lodash/isArray");
var SchemaSieve_1 = require("./components/Filters/SchemaSieve");
var convertToBooleanSignature = function (legacyFilter) {
    return {
        field: legacyFilter.name,
        value: legacyFilter.operator === 'is true',
        operator: 'is',
    };
};
var convertToStringSignature = function (legacyFilter) {
    var signature = {
        field: legacyFilter.name,
        value: legacyFilter.value,
        operator: '',
    };
    switch (legacyFilter.operator) {
        case 'does not contain':
            signature.operator = 'not_contains';
            break;
        case 'matches RegEx':
            signature.operator = 'matches_re';
            break;
        case 'does not match RegEx':
            signature.operator = 'not_matches_re';
            break;
        default:
            signature.operator = legacyFilter.operator || '';
            break;
    }
    return signature;
};
var convertToDateTimeSignature = function (legacyFilter) {
    var signature = {
        field: legacyFilter.name,
        value: legacyFilter.value,
        operator: '',
    };
    switch (legacyFilter.operator) {
        case 'is after':
            signature.operator = 'is_after';
            break;
        case 'is before':
            signature.operator = 'is_before';
            break;
        default:
            signature.operator = legacyFilter.operator || '';
            break;
    }
    return signature;
};
var convertToEnumSignature = function (legacyFilter) {
    var signature = {
        field: legacyFilter.name,
        value: legacyFilter.value,
        operator: '',
    };
    switch (legacyFilter.operator) {
        case 'is not':
            signature.operator = 'is_not';
            break;
        default:
            signature.operator = legacyFilter.operator || '';
            break;
    }
    return signature;
};
var convertToNumberSignature = function (legacyFilter) {
    var signature = {
        field: legacyFilter.name,
        value: legacyFilter.value,
        operator: '',
    };
    switch (legacyFilter.operator) {
        case 'equals':
            signature.operator = 'is';
            break;
        case 'more than':
            signature.operator = 'is_more_than';
            break;
        case 'less than':
            signature.operator = 'is_less_than';
            break;
        default:
            signature.operator = legacyFilter.operator || '';
            break;
    }
    return signature;
};
var convertToObjectSignature = function (legacyFilter) {
    var signature = {
        field: legacyFilter.name,
        value: legacyFilter.value,
        operator: '',
    };
    switch (legacyFilter.operator) {
        case 'is':
            signature.operator = 'is';
            break;
        case 'is not':
            signature.operator = 'is_not';
            break;
        case 'key is':
            signature.operator = 'key_is';
            break;
        case 'key contains':
            signature.operator = 'key_contains';
            break;
        case 'key does not contain':
            signature.operator = 'key_not_contains';
            break;
        case 'key matches RegEx':
            signature.operator = 'key_matches_re';
            break;
        case 'key does not match RegEx':
            signature.operator = 'key_not_matches_re';
            break;
        case 'value is':
            signature.operator = 'value_is';
            break;
        case 'value contains':
            signature.operator = 'value_contains';
            break;
        case 'value does not contain':
            signature.operator = 'value_not_contains';
            break;
        case 'value matches RegEx':
            signature.operator = 'value_matches_re';
            break;
        case 'value does not match RegEx':
            signature.operator = 'value_not_matches_re';
            break;
        default:
            signature.operator = legacyFilter.operator || '';
            break;
    }
    return signature;
};
var getLegacyFilterSignature = function (legacyFilter) {
    switch (legacyFilter.type) {
        case 'Boolean':
            return convertToBooleanSignature(legacyFilter);
        case 'Case Insensitive Text':
        case 'Short Text':
        case 'Text':
            return convertToStringSignature(legacyFilter);
        case 'Date Time':
        case 'Date':
        case 'Time':
            return convertToDateTimeSignature(legacyFilter);
        case 'Enum':
            return convertToEnumSignature(legacyFilter);
        case 'Integer':
        case 'Real':
            return convertToNumberSignature(legacyFilter);
        case 'Key Value Pair':
            return convertToObjectSignature(legacyFilter);
        default:
            return null;
    }
};
exports.migrateLegacyFilter = function (schema, legacyFilter) {
    if (!legacyFilter.name) {
        return legacyFilter;
    }
    var filter = legacyFilter;
    if (filter.name === 'Full text search') {
        return SchemaSieve_1.createFullTextSearchFilter(schema, filter.value);
    }
    var signatures = [];
    signatures.push(getLegacyFilterSignature(filter));
    if (filter.extra) {
        var extraSignatures = filter.extra.or.map(getLegacyFilterSignature);
        Array.prototype.push.apply(signatures, extraSignatures);
    }
    return SchemaSieve_1.createFilter(schema, signatures.filter(function (s) { return s !== null; }));
};
exports.migrateLegacyViews = function (schema, legacyViews) {
    if (isArray(legacyViews.views) &&
        isArray(legacyViews.viewScopes)) {
        return legacyViews;
    }
    var views = [];
    var viewScopes = [];
    legacyViews.forEach(function (scope) {
        if (scope.filters) {
            views.push(scope);
            return;
        }
        viewScopes.push({
            slug: scope.key,
            name: scope.title || '',
            label: scope.scopeLabel,
        });
        scope.data.forEach(function (legacyView) {
            views.push({
                id: legacyView.id,
                name: legacyView.name,
                scope: legacyView.scopeKey,
                filters: legacyView.rules.map(function (rule) {
                    return exports.migrateLegacyFilter(schema, rule);
                }),
            });
        });
    });
    return {
        views: views,
        viewScopes: viewScopes,
    };
};
exports.migrateLegacySchema = function (legacySchema) {
    var schema = {
        type: 'object',
        properties: {},
    };
    forEach(legacySchema, function (item, key) {
        switch (item.type) {
            case 'Boolean':
                schema.properties[key] = {
                    type: 'boolean',
                };
                break;
            case 'Case Insensitive Text':
            case 'Short Text':
            case 'Text':
                schema.properties[key] = {
                    type: 'string',
                };
                break;
            case 'Date Time':
            case 'Date':
            case 'Time':
                schema.properties[key] = {
                    type: 'string',
                    format: 'date-time',
                };
                break;
            case 'Enum':
                schema.properties[key] = {
                    enum: item.values,
                };
                break;
            case 'Integer':
            case 'Real':
                schema.properties[key] = {
                    type: 'number',
                };
                break;
            case 'Key Value Pair':
                schema.properties[key] = {
                    type: 'object',
                    properties: (_a = {},
                        _a[item.key] = {
                            title: item.keyLabel,
                            description: 'key',
                            type: 'string',
                        },
                        _a[item.value] = {
                            title: item.valueLabel,
                            description: 'value',
                            type: 'string',
                        },
                        _a),
                };
                break;
            default:
                return;
        }
        if (item.label) {
            schema.properties[key].title = item.label;
        }
        var _a;
    });
    return schema;
};
