"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseDataCellCopy = void 0;
const lodash_1 = require("lodash");
const common_1 = require("../../../common");
const export_1 = require("../../../common/interface/export");
const method_1 = require("../method");
const common_2 = require("./common");
class BaseDataCellCopy {
    initIdleCallbackCount(rowLength) {
        this.idleCallbackCount =
            rowLength >= common_1.AsyncRenderThreshold ? common_1.AsyncRenderThreshold : rowLength;
    }
    constructor(params) {
        this.matrixHtmlTransformer = (dataMatrix) => {
            return this.config.transformers[export_1.CopyMIMEType.HTML](dataMatrix);
        };
        const { spreadsheet, isExport = false, config } = params;
        this.spreadsheet = spreadsheet;
        this.config = (0, common_2.unifyConfig)({ config, spreadsheet, isExport });
    }
    getHeaderNodeMatrix(node) {
        const { formatHeader } = this.config;
        return (0, method_1.getHeaderMeasureFieldNames)((0, method_1.getHeaderList)(node.id), node.spreadsheet, formatHeader);
    }
    matrixPlainTextTransformer(dataMatrix, separator) {
        const escapeDataMatrix = (0, lodash_1.map)(dataMatrix, (row) => (0, lodash_1.map)(row, method_1.escapeField));
        return this.config.transformers[export_1.CopyMIMEType.PLAIN](escapeDataMatrix, separator);
    }
    matrixTransformer(dataMatrix, separator = common_1.TAB_SEPARATOR) {
        return [
            this.matrixPlainTextTransformer(dataMatrix, separator),
            this.matrixHtmlTransformer(dataMatrix),
        ];
    }
    getFormatter(options) {
        const { field, rowIndex, colIndex } = options;
        if (this.config.formatData) {
            const viewMeta = this.spreadsheet.facet.getCellMeta(rowIndex, colIndex);
            return (value) => {
                const formatter = this.spreadsheet.dataSet.getFieldFormatter(field);
                return formatter === null || formatter === void 0 ? void 0 : formatter(value, viewMeta === null || viewMeta === void 0 ? void 0 : viewMeta.data, viewMeta);
            };
        }
        return ((value) => value);
    }
    /**
     * Safari 等不支持 requestIdleCallback 的浏览器, 降级为同步
     */
    isEnableASync() {
        return this.config.async && !!window.requestIdleCallback;
    }
}
exports.BaseDataCellCopy = BaseDataCellCopy;
//# sourceMappingURL=base-data-cell-copy.js.map