
/*
* 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.
 */

import { SERIES_LAYOUT_BY_COLUMN } from '../../util/types';
import { normalizeToArray } from '../../util/model';
import { assert, createHashMap, bind, each, hasOwn, map, clone, isObject, isArrayLike } from 'zrender/esm/core/util';
import { getRawSourceItemGetter, getRawSourceDataCounter, getRawSourceValueGetter } from './dataProvider';
import { parseDataValue } from './dataValueHelper';
import { createSource, inheritSourceMetaRawOption } from './sourceHelper';
import { consoleLog, makePrintable } from '../../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 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 = {};
  each(dimsDef, function (dimDef, idx) {
    var name = dimDef.name;
    var dimDefExt = {
      index: idx,
      name: name,
      displayName: dimDef.displayName
    };
    dimensions.push(dimDefExt);

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

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

  var rawCounter = getRawSourceDataCounter(sourceFormat, SERIES_LAYOUT_BY_COLUMN);
  extSource.count = bind(rawCounter, null, data, sourceHeaderCount, dimensions);
  var rawValueGetter = 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 = bind(getDimensionInfo, null, dimensions, dimsByName);
  extSource.getDimensionInfoAll = bind(getDimensionInfoAll, null, dimensions);
  return extSource;
}

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

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

function getDimensionInfoAll(dimensions) {
  return dimensions;
}

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

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

  externalTransformMap.set(type, externalTransform);
}
export function applyDataTransform(rawTransOption, sourceList, infoForPrint) {
  var pipedTransOption = 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;
}

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

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

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