
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/



/**
 * AUTO-GENERATED FILE. DO NOT MODIFY.
 */

"use strict";

exports.__esModule = true;

var types_1 = require("../../util/types");

var model_1 = require("../../util/model");

var util_1 = require("zrender/lib/core/util");

var dataProvider_1 = require("./dataProvider");

var dataValueHelper_1 = require("./dataValueHelper");

var sourceHelper_1 = require("./sourceHelper");

var log_1 = require("../../util/log");

var ExternalSource = function () {
  function ExternalSource() {}

  ExternalSource.prototype.getDimensionInfo = function (dim) {
    return;
  };

  ExternalSource.prototype.getDimensionInfoAll = function () {
    return;
  };

  ExternalSource.prototype.getRawDataItem = function (dataIndex) {
    return;
  };

  ExternalSource.prototype.getRawHeaderItem = function (dataIndex) {
    return;
  };

  ExternalSource.prototype.count = function () {
    return;
  };

  ExternalSource.prototype.retrieveItemValue = function (rawItem, dimIndex) {
    return;
  };

  ExternalSource.prototype.convertDataValue = function (rawVal, dimInfo) {
    return dataValueHelper_1.parseDataValue(rawVal, dimInfo);
  };

  return ExternalSource;
}();

function createExternalSource(data, sourceFormat, dimsDef, sourceHeaderCount) {
  var extSource = new ExternalSource();
  extSource.data = data;
  extSource.sourceFormat = sourceFormat;
  extSource.sourceHeaderCount = sourceHeaderCount;
  var dimensions = [];
  var dimsByName = {};
  util_1.each(dimsDef, function (dimDef, idx) {
    var name = dimDef.name;
    var dimDefExt = {
      index: idx,
      name: name,
      displayName: dimDef.displayName
    };
    dimensions.push(dimDefExt);

    if (name != null) {
      util_1.assert(!util_1.hasOwn(dimsByName, name), 'dimension name "' + name + '" duplicated.');
      dimsByName[name] = dimDefExt;
    }
  });
  var rawItemGetter = dataProvider_1.getRawSourceItemGetter(sourceFormat, types_1.SERIES_LAYOUT_BY_COLUMN);
  extSource.getRawDataItem = util_1.bind(rawItemGetter, null, data, sourceHeaderCount, dimensions);

  extSource.getRawHeaderItem = function (dataIndex) {
    if (dataIndex < sourceHeaderCount) {
      return rawItemGetter(data, 0, dimensions, dataIndex);
    }
  };

  var rawCounter = dataProvider_1.getRawSourceDataCounter(sourceFormat, types_1.SERIES_LAYOUT_BY_COLUMN);
  extSource.count = util_1.bind(rawCounter, null, data, sourceHeaderCount, dimensions);
  var rawValueGetter = dataProvider_1.getRawSourceValueGetter(sourceFormat);

  extSource.retrieveItemValue = function (rawItem, dimIndex) {
    if (rawItem == null) {
      return;
    }

    var dimDef = dimensions[dimIndex];

    if (dimDef) {
      return rawValueGetter(rawItem, dimIndex, dimDef.name);
    }
  };

  extSource.getDimensionInfo = util_1.bind(getDimensionInfo, null, dimensions, dimsByName);
  extSource.getDimensionInfoAll = util_1.bind(getDimensionInfoAll, null, dimensions);
  return extSource;
}

function getDimensionInfo(dimensions, dimsByName, dim) {
  if (dim == null) {
    return;
  }

  if (typeof dim === 'number' || !isNaN(dim) && !util_1.hasOwn(dimsByName, dim)) {
    return dimensions[dim];
  } else if (util_1.hasOwn(dimsByName, dim)) {
    return dimsByName[dim];
  }
}

function getDimensionInfoAll(dimensions) {
  return dimensions;
}

var externalTransformMap = util_1.createHashMap();

function registerExternalTransform(externalTransform) {
  externalTransform = util_1.clone(externalTransform);
  var type = externalTransform.type;
  util_1.assert(type, 'Must have a `type` when `registerTransform`.');
  var typeParsed = type.split(':');
  util_1.assert(typeParsed.length === 2, 'Name must include namespace like "ns:regression".');

  if (typeParsed[0] === 'echarts') {
    type = typeParsed[1];
  }

  externalTransformMap.set(type, externalTransform);
}

exports.registerExternalTransform = registerExternalTransform;

function applyDataTransform(rawTransOption, sourceList, infoForPrint) {
  var pipedTransOption = model_1.normalizeToArray(rawTransOption);

  for (var i = 0, len = pipedTransOption.length; i < len; i++) {
    var transOption = pipedTransOption[i];
    var isFinal = i === len - 1;
    sourceList = applySingleDataTransform(transOption, sourceList, infoForPrint, isFinal);

    if (!isFinal) {
      sourceList.length = Math.max(sourceList.length, 1);
    }
  }

  return sourceList;
}

exports.applyDataTransform = applyDataTransform;

function applySingleDataTransform(rawTransOption, upSourceList, infoForPrint, isFinal) {
  util_1.assert(upSourceList.length, 'Must have at least one upstream dataset.');
  var transOption = rawTransOption;
  var transType = transOption.type;
  var externalTransform = externalTransformMap.get(transType);
  util_1.assert(externalTransform, 'Can not find transform on type "' + transType + '".');
  var sourceList = util_1.map(upSourceList, function (source) {
    return createExternalSource(source.data, source.sourceFormat, source.dimensionsDefine, source.startIndex);
  });
  var resultList = model_1.normalizeToArray(externalTransform.transform({
    source: sourceList[0],
    sourceList: sourceList,
    config: util_1.clone(transOption.config)
  }));

  if (process.env.NODE_ENV !== 'production') {
    if (isFinal && transOption.print) {
      var printStrArr = util_1.map(resultList, function (extSource) {
        return ['--- datasetIndex: ' + infoForPrint.datasetIndex + '---', '- transform result data:', log_1.makePrintable(extSource.data), '- transform result dimensions:', log_1.makePrintable(extSource.dimensions), '- transform result sourceHeader: ' + extSource.sourceHeader, '------'].join('\n');
      }).join('\n');
      log_1.consoleLog(printStrArr);
    }
  }

  return util_1.map(resultList, function (result) {
    util_1.assert(util_1.isObject(result), 'A transform should not return some empty results.');
    util_1.assert(util_1.isObject(result.data) || util_1.isArrayLike(result.data), 'Result data should be object or array in data transform.');
    var resultMetaRawOption = sourceHelper_1.inheritSourceMetaRawOption({
      parent: upSourceList[0].metaRawOption,
      thisNew: {
        seriesLayoutBy: types_1.SERIES_LAYOUT_BY_COLUMN,
        sourceHeader: result.sourceHeader,
        dimensions: result.dimensions
      }
    });
    return sourceHelper_1.createSource(result.data, resultMetaRawOption, null, null);
  });
}