"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const types = require("../../util/types.js");
const model = require("../../util/model.js");
const util = require("../../../../../../zrender@5.4.4/node_modules/zrender/lib/core/util.js");
const dataProvider = require("./dataProvider.js");
const dataValueHelper = require("./dataValueHelper.js");
const log = require("../../util/log.js");
const Source = require("../Source.js");
var ExternalSource = (
  /** @class */
  function() {
    function ExternalSource2() {
    }
    ExternalSource2.prototype.getRawData = function() {
      throw new Error("not supported");
    };
    ExternalSource2.prototype.getRawDataItem = function(dataIndex) {
      throw new Error("not supported");
    };
    ExternalSource2.prototype.cloneRawData = function() {
      return;
    };
    ExternalSource2.prototype.getDimensionInfo = function(dim) {
      return;
    };
    ExternalSource2.prototype.cloneAllDimensionInfo = function() {
      return;
    };
    ExternalSource2.prototype.count = function() {
      return;
    };
    ExternalSource2.prototype.retrieveValue = function(dataIndex, dimIndex) {
      return;
    };
    ExternalSource2.prototype.retrieveValueFromItem = function(dataItem, dimIndex) {
      return;
    };
    ExternalSource2.prototype.convertValue = function(rawVal, dimInfo) {
      return dataValueHelper.parseDataValue(rawVal, dimInfo);
    };
    return ExternalSource2;
  }()
);
function createExternalSource(internalSource, externalTransform) {
  var extSource = new ExternalSource();
  var data = internalSource.data;
  var sourceFormat = extSource.sourceFormat = internalSource.sourceFormat;
  var sourceHeaderCount = internalSource.startIndex;
  var errMsg = "";
  if (internalSource.seriesLayoutBy !== types.SERIES_LAYOUT_BY_COLUMN) {
    if (process.env.NODE_ENV !== "production") {
      errMsg = '`seriesLayoutBy` of upstream dataset can only be "column" in data transform.';
    }
    log.throwError(errMsg);
  }
  var dimensions = [];
  var dimsByName = {};
  var dimsDef = internalSource.dimensionsDefine;
  if (dimsDef) {
    util.each(dimsDef, function(dimDef, idx) {
      var name = dimDef.name;
      var dimDefExt = {
        index: idx,
        name,
        displayName: dimDef.displayName
      };
      dimensions.push(dimDefExt);
      if (name != null) {
        var errMsg_1 = "";
        if (util.hasOwn(dimsByName, name)) {
          if (process.env.NODE_ENV !== "production") {
            errMsg_1 = 'dimension name "' + name + '" duplicated.';
          }
          log.throwError(errMsg_1);
        }
        dimsByName[name] = dimDefExt;
      }
    });
  } else {
    for (var i = 0; i < internalSource.dimensionsDetectedCount || 0; i++) {
      dimensions.push({
        index: i
      });
    }
  }
  var rawItemGetter = dataProvider.getRawSourceItemGetter(sourceFormat, types.SERIES_LAYOUT_BY_COLUMN);
  if (externalTransform.__isBuiltIn) {
    extSource.getRawDataItem = function(dataIndex) {
      return rawItemGetter(data, sourceHeaderCount, dimensions, dataIndex);
    };
    extSource.getRawData = util.bind(getRawData, null, internalSource);
  }
  extSource.cloneRawData = util.bind(cloneRawData, null, internalSource);
  var rawCounter = dataProvider.getRawSourceDataCounter(sourceFormat, types.SERIES_LAYOUT_BY_COLUMN);
  extSource.count = util.bind(rawCounter, null, data, sourceHeaderCount, dimensions);
  var rawValueGetter = dataProvider.getRawSourceValueGetter(sourceFormat);
  extSource.retrieveValue = function(dataIndex, dimIndex) {
    var rawItem = rawItemGetter(data, sourceHeaderCount, dimensions, dataIndex);
    return retrieveValueFromItem(rawItem, dimIndex);
  };
  var retrieveValueFromItem = extSource.retrieveValueFromItem = function(dataItem, dimIndex) {
    if (dataItem == null) {
      return;
    }
    var dimDef = dimensions[dimIndex];
    if (dimDef) {
      return rawValueGetter(dataItem, dimIndex, dimDef.name);
    }
  };
  extSource.getDimensionInfo = util.bind(getDimensionInfo, null, dimensions, dimsByName);
  extSource.cloneAllDimensionInfo = util.bind(cloneAllDimensionInfo, null, dimensions);
  return extSource;
}
function getRawData(upstream) {
  var sourceFormat = upstream.sourceFormat;
  if (!isSupportedSourceFormat(sourceFormat)) {
    var errMsg = "";
    if (process.env.NODE_ENV !== "production") {
      errMsg = "`getRawData` is not supported in source format " + sourceFormat;
    }
    log.throwError(errMsg);
  }
  return upstream.data;
}
function cloneRawData(upstream) {
  var sourceFormat = upstream.sourceFormat;
  var data = upstream.data;
  if (!isSupportedSourceFormat(sourceFormat)) {
    var errMsg = "";
    if (process.env.NODE_ENV !== "production") {
      errMsg = "`cloneRawData` is not supported in source format " + sourceFormat;
    }
    log.throwError(errMsg);
  }
  if (sourceFormat === types.SOURCE_FORMAT_ARRAY_ROWS) {
    var result = [];
    for (var i = 0, len = data.length; i < len; i++) {
      result.push(data[i].slice());
    }
    return result;
  } else if (sourceFormat === types.SOURCE_FORMAT_OBJECT_ROWS) {
    var result = [];
    for (var i = 0, len = data.length; i < len; i++) {
      result.push(util.extend({}, data[i]));
    }
    return result;
  }
}
function getDimensionInfo(dimensions, dimsByName, dim) {
  if (dim == null) {
    return;
  }
  if (util.isNumber(dim) || !isNaN(dim) && !util.hasOwn(dimsByName, dim)) {
    return dimensions[dim];
  } else if (util.hasOwn(dimsByName, dim)) {
    return dimsByName[dim];
  }
}
function cloneAllDimensionInfo(dimensions) {
  return util.clone(dimensions);
}
var externalTransformMap = util.createHashMap();
function registerExternalTransform(externalTransform) {
  externalTransform = util.clone(externalTransform);
  var type = externalTransform.type;
  var errMsg = "";
  if (!type) {
    if (process.env.NODE_ENV !== "production") {
      errMsg = "Must have a `type` when `registerTransform`.";
    }
    log.throwError(errMsg);
  }
  var typeParsed = type.split(":");
  if (typeParsed.length !== 2) {
    if (process.env.NODE_ENV !== "production") {
      errMsg = 'Name must include namespace like "ns:regression".';
    }
    log.throwError(errMsg);
  }
  var isBuiltIn = false;
  if (typeParsed[0] === "echarts") {
    type = typeParsed[1];
    isBuiltIn = true;
  }
  externalTransform.__isBuiltIn = isBuiltIn;
  externalTransformMap.set(type, externalTransform);
}
function applyDataTransform(rawTransOption, sourceList, infoForPrint) {
  var pipedTransOption = model.normalizeToArray(rawTransOption);
  var pipeLen = pipedTransOption.length;
  var errMsg = "";
  if (!pipeLen) {
    if (process.env.NODE_ENV !== "production") {
      errMsg = "If `transform` declared, it should at least contain one transform.";
    }
    log.throwError(errMsg);
  }
  for (var i = 0, len = pipeLen; i < len; i++) {
    var transOption = pipedTransOption[i];
    sourceList = applySingleDataTransform(transOption, sourceList, infoForPrint, pipeLen === 1 ? null : i);
    if (i !== len - 1) {
      sourceList.length = Math.max(sourceList.length, 1);
    }
  }
  return sourceList;
}
function applySingleDataTransform(transOption, upSourceList, infoForPrint, pipeIndex) {
  var errMsg = "";
  if (!upSourceList.length) {
    if (process.env.NODE_ENV !== "production") {
      errMsg = "Must have at least one upstream dataset.";
    }
    log.throwError(errMsg);
  }
  if (!util.isObject(transOption)) {
    if (process.env.NODE_ENV !== "production") {
      errMsg = "transform declaration must be an object rather than " + typeof transOption + ".";
    }
    log.throwError(errMsg);
  }
  var transType = transOption.type;
  var externalTransform = externalTransformMap.get(transType);
  if (!externalTransform) {
    if (process.env.NODE_ENV !== "production") {
      errMsg = 'Can not find transform on type "' + transType + '".';
    }
    log.throwError(errMsg);
  }
  var extUpSourceList = util.map(upSourceList, function(upSource) {
    return createExternalSource(upSource, externalTransform);
  });
  var resultList = model.normalizeToArray(externalTransform.transform({
    upstream: extUpSourceList[0],
    upstreamList: extUpSourceList,
    config: util.clone(transOption.config)
  }));
  if (process.env.NODE_ENV !== "production") {
    if (transOption.print) {
      var printStrArr = util.map(resultList, function(extSource) {
        var pipeIndexStr = pipeIndex != null ? " === pipe index: " + pipeIndex : "";
        return ["=== dataset index: " + infoForPrint.datasetIndex + pipeIndexStr + " ===", "- transform result data:", log.makePrintable(extSource.data), "- transform result dimensions:", log.makePrintable(extSource.dimensions)].join("\n");
      }).join("\n");
      log.log(printStrArr);
    }
  }
  return util.map(resultList, function(result, resultIndex) {
    var errMsg2 = "";
    if (!util.isObject(result)) {
      if (process.env.NODE_ENV !== "production") {
        errMsg2 = "A transform should not return some empty results.";
      }
      log.throwError(errMsg2);
    }
    if (!result.data) {
      if (process.env.NODE_ENV !== "production") {
        errMsg2 = "Transform result data should be not be null or undefined";
      }
      log.throwError(errMsg2);
    }
    var sourceFormat = Source.detectSourceFormat(result.data);
    if (!isSupportedSourceFormat(sourceFormat)) {
      if (process.env.NODE_ENV !== "production") {
        errMsg2 = "Transform result data should be array rows or object rows.";
      }
      log.throwError(errMsg2);
    }
    var resultMetaRawOption;
    var firstUpSource = upSourceList[0];
    if (firstUpSource && resultIndex === 0 && !result.dimensions) {
      var startIndex = firstUpSource.startIndex;
      if (startIndex) {
        result.data = firstUpSource.data.slice(0, startIndex).concat(result.data);
      }
      resultMetaRawOption = {
        seriesLayoutBy: types.SERIES_LAYOUT_BY_COLUMN,
        sourceHeader: startIndex,
        dimensions: firstUpSource.metaRawOption.dimensions
      };
    } else {
      resultMetaRawOption = {
        seriesLayoutBy: types.SERIES_LAYOUT_BY_COLUMN,
        sourceHeader: 0,
        dimensions: result.dimensions
      };
    }
    return Source.createSource(result.data, resultMetaRawOption, null);
  });
}
function isSupportedSourceFormat(sourceFormat) {
  return sourceFormat === types.SOURCE_FORMAT_ARRAY_ROWS || sourceFormat === types.SOURCE_FORMAT_OBJECT_ROWS;
}
exports.ExternalSource = ExternalSource;
exports.applyDataTransform = applyDataTransform;
exports.registerExternalTransform = registerExternalTransform;
