"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.scanPLSFiles = void 0;
var tslib_1 = require("tslib");
var _ = require('lodash');
var getProductName = function (ctx, productId) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var products, pLists, pRecords_1, pNames_1;
    return tslib_1.__generator(this, function (_a) {
        switch (_a.label) {
            case 0:
                products = _.split(productId, '-');
                return [4, ctx.getObject('picklists').directFind({ filters: ["code", "=",
                            "223321001114213"] })];
            case 1:
                pLists = _a.sent();
                if (!(products.length > 0 && pLists.length > 0)) return [3, 3];
                return [4, ctx.getObject('picklist_options').directFind({
                        filters: [
                            ["picklist", "=", pLists[0]._id],
                            ["value", "in", products]
                        ]
                    })];
            case 2:
                pRecords_1 = _a.sent();
                pNames_1 = [];
                _.each(products, function (pId) {
                    var pRecord = _.find(pRecords_1, { value: pId });
                    if (pRecord) {
                        pNames_1.push(pRecord.name);
                    }
                });
                return [2, _.join(pNames_1, '-')];
            case 3: return [2, ''];
        }
    });
}); };
var scanPLSFiles = function (ctx) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var approvalId, approvals, addPLSCount, approvals_1, approvals_1_1, approval, productId, productName, materials, materials_1, materials_1_1, material, filters, files, files_1, files_1_1, file, e_1_1, e_2_1, _a, _b, fileId, e_3_1, error_1, e_4_1;
    var e_4, _c, e_2, _d, e_1, _e, e_3, _f;
    return tslib_1.__generator(this, function (_g) {
        switch (_g.label) {
            case 0:
                approvalId = "";
                console.error("[scan pls files]: start ", new Date());
                return [4, ctx.getObject('pepsico_material_approval').directFind({ filters: [['current_step', 'in', ['awaiting_deployment', 'conclusion']], ['scanned_pls', '!=', true]] })];
            case 1:
                approvals = _g.sent();
                console.error("[scan pls files]: approvals.length", approvals.length);
                addPLSCount = 0;
                _g.label = 2;
            case 2:
                _g.trys.push([2, 35, 36, 37]);
                approvals_1 = tslib_1.__values(approvals), approvals_1_1 = approvals_1.next();
                _g.label = 3;
            case 3:
                if (!!approvals_1_1.done) return [3, 34];
                approval = approvals_1_1.value;
                _g.label = 4;
            case 4:
                _g.trys.push([4, 31, , 33]);
                approvalId = approval._id;
                productId = approval.product;
                return [4, getProductName(ctx, productId)];
            case 5:
                productName = _g.sent();
                return [4, ctx.getObject('pepsico_material').directFind({
                        filters: [
                            ['pepsico_material_approval', '=', approvalId]
                        ]
                    })];
            case 6:
                materials = _g.sent();
                console.error("[scan pls files]: approvalId", approvalId);
                _g.label = 7;
            case 7:
                _g.trys.push([7, 19, 20, 21]);
                materials_1 = (e_2 = void 0, tslib_1.__values(materials)), materials_1_1 = materials_1.next();
                _g.label = 8;
            case 8:
                if (!!materials_1_1.done) return [3, 18];
                material = materials_1_1.value;
                filters = [
                    ['metadata/record_id', '=', "objects:pepsico_material:".concat(material._id)],
                    ['metadata/object_name', '=', 'b6_rooms'],
                    ['original/type', '=', 'application/pdf'],
                    ['original/name', 'contains', 'PLS']
                ];
                console.error("[scan pls files]: approvalId", approvalId, "material-".concat(material._id), 'filters: ', JSON.stringify(filters));
                return [4, ctx.getObject('cfs_files_filerecord').directFind({
                        filters: filters
                    })];
            case 9:
                files = _g.sent();
                console.error("[scan pls files]: approvalId", approvalId, "material-".concat(material._id), 'files.length: ', files.length);
                _g.label = 10;
            case 10:
                _g.trys.push([10, 15, 16, 17]);
                files_1 = (e_1 = void 0, tslib_1.__values(files)), files_1_1 = files_1.next();
                _g.label = 11;
            case 11:
                if (!!files_1_1.done) return [3, 14];
                file = files_1_1.value;
                return [4, ctx.getObject('pepsico_km_pls_file').directInsert({
                        name: productName,
                        ai_extract: false,
                        pls_file: file._id,
                        pepsico_material: material._id,
                        product: productId,
                        pepsico_material_approval: approvalId,
                        space: approval.space,
                        created: new Date(),
                        modified: new Date()
                    })];
            case 12:
                _g.sent();
                addPLSCount++;
                _g.label = 13;
            case 13:
                files_1_1 = files_1.next();
                return [3, 11];
            case 14: return [3, 17];
            case 15:
                e_1_1 = _g.sent();
                e_1 = { error: e_1_1 };
                return [3, 17];
            case 16:
                try {
                    if (files_1_1 && !files_1_1.done && (_e = files_1.return)) _e.call(files_1);
                }
                finally { if (e_1) throw e_1.error; }
                return [7];
            case 17:
                materials_1_1 = materials_1.next();
                return [3, 8];
            case 18: return [3, 21];
            case 19:
                e_2_1 = _g.sent();
                e_2 = { error: e_2_1 };
                return [3, 21];
            case 20:
                try {
                    if (materials_1_1 && !materials_1_1.done && (_d = materials_1.return)) _d.call(materials_1);
                }
                finally { if (e_2) throw e_2.error; }
                return [7];
            case 21:
                if (!(approval.pls_file && approval.pls_file.length > 0)) return [3, 29];
                _g.label = 22;
            case 22:
                _g.trys.push([22, 27, 28, 29]);
                _a = (e_3 = void 0, tslib_1.__values(approval.pls_file)), _b = _a.next();
                _g.label = 23;
            case 23:
                if (!!_b.done) return [3, 26];
                fileId = _b.value;
                return [4, ctx.getObject('pepsico_km_pls_file').directInsert({
                        name: productName,
                        ai_extract: false,
                        pls_file: fileId,
                        product: productId,
                        pepsico_material_approval: approvalId,
                        space: approval.space,
                        created_by: approval.created_by,
                        created: new Date(),
                        modified: new Date()
                    })];
            case 24:
                _g.sent();
                _g.label = 25;
            case 25:
                _b = _a.next();
                return [3, 23];
            case 26: return [3, 29];
            case 27:
                e_3_1 = _g.sent();
                e_3 = { error: e_3_1 };
                return [3, 29];
            case 28:
                try {
                    if (_b && !_b.done && (_f = _a.return)) _f.call(_a);
                }
                finally { if (e_3) throw e_3.error; }
                return [7];
            case 29: return [4, ctx.getObject('pepsico_material_approval').directUpdate(approvalId, {
                    scanned_pls: true,
                    scanned_pls_date: new Date()
                })];
            case 30:
                _g.sent();
                return [3, 33];
            case 31:
                error_1 = _g.sent();
                console.error("[scan pls files]-".concat(approvalId), error_1, new Date());
                return [4, ctx.logger.error("[scan pls files]-".concat(approvalId), error_1.message)];
            case 32:
                _g.sent();
                return [3, 33];
            case 33:
                approvals_1_1 = approvals_1.next();
                return [3, 3];
            case 34: return [3, 37];
            case 35:
                e_4_1 = _g.sent();
                e_4 = { error: e_4_1 };
                return [3, 37];
            case 36:
                try {
                    if (approvals_1_1 && !approvals_1_1.done && (_c = approvals_1.return)) _c.call(approvals_1);
                }
                finally { if (e_4) throw e_4.error; }
                return [7];
            case 37: return [2, addPLSCount];
        }
    });
}); };
exports.scanPLSFiles = scanPLSFiles;
//# sourceMappingURL=pls.js.map