"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OjFilePicker = void 0;
const OjFilePickerBase_1 = require("./OjFilePickerBase");
const fs = require("fs");
const path = require("path");
/**
 * The component WebElement for [oj-file-picker](../../jsdocs/oj.ojFilePicker.html).
 * Do not instantiate this class directly, instead, use
 * [ojFilePicker](../functions/elements.ojFilePicker-1.html).
 */
class OjFilePicker extends OjFilePickerBase_1.OjFilePickerBase {
    /**
     * Takes an Array of objects containing file paths + types.
     * These files will be read from the local filesystem and then sent
     * to the oj-file-picker to simulate user file selection. Only the basename of
     * the file will be sent, not the entire path to make it consistent with how
     * the browser sets the value.
     * @param files An array of objects containing the path and type of selected files
     */
    async doSelect(files) {
        await this.whenReady();
        let dataList;
        dataList = [];
        let file;
        for (let i = 0; i < files.length; i++) {
            const fileInfo = files[i];
            const filePath = fileInfo.path;
            const fileType = fileInfo.type;
            const data = fs.readFileSync(filePath);
            if (data) {
                file = { bytes: Array.from(data), path: path.basename(filePath), type: fileType };
                dataList.push(file);
            }
        }
        await this.getDriver().executeAsyncScript([
            'var fileList = [];',
            'var list = arguments[1];',
            'for(var i = 0; i < list.length; i++) {',
            'var data = list[i];',
            'fileList.push(new File([Uint8Array.from(data.bytes)], data.path, {type: data.type}));',
            '}',
            'fileList.item = function(i){return fileList[i];}',
            //arguments[2] is a callback automatically passed in to continue executing the code
            'arguments[0]._doSelectHelper(fileList).then(arguments[2]);'
        ].join('\n'), this, dataList);
        await this.whenBusyContextReady();
    }
}
exports.OjFilePicker = OjFilePicker;
//# sourceMappingURL=OjFilePicker.js.map