"use strict";

const scrape = require("./scrape");

class Scrapist {

  constructor(scheme, config) {
    this._scheme = scheme;
    this._pages = scheme.pages;
    this._after = scheme.after;
    this._rootUrl = scheme.rootUrl;
    this._defaultParam = scheme.defaultParam;
    this._config = Object.assign({}, Scrapist.defaultConfig, config);
  }

  get scheme() {
    return this._scheme;
  }

  get config() {
    return this._config;
  }

  getRootUrl(param) {
    if (typeof this._rootUrl === "function")
      return this._rootUrl(param);
    if (typeof this._rootUrl === "string")
      return this._rootUrl;
    return param;
  }

  afterScrape(data) {
    if (this._after)
      return this._after(data);
    return data;
  }

  scrape(param, config) {

    const p = typeof param === "object" && this._defaultParam ?
      Object.assign({}, this._defaultParam, param) :
      typeof param === "undefined" ? this._defaultParam : param;

    return scrape(
      this.getRootUrl(p),
      p,
      this._pages,
      Object.assign({}, this._config, config)
    ).then(data => this.afterScrape(data));
  }

}

Scrapist.defaultConfig = {
  concurrency: 1,
  interval: 1000,
  trial: 1,
  trialInterval: 1000,
  beforeFetch: null,
  onData: null,
  onError: () => true,
  fetch: null
};

module.exports = Scrapist;

@@ -940,16 +940,193 @@
 param;%0A%0A
+    const conf = Object.assign(%7B%7D, this._config, config);%0A%0A    if (typeof conf.fetch !== %22function%22) %7B%0A      throw new Error(%22scrapist: config.fetch is not a function%22);%0A    %7D%0A%0A
     retu
@@ -1136,16 +1136,16 @@
 scrape(%0A
-
       th
@@ -1200,47 +1200,12 @@
     
-Object.assign(%7B%7D, this._config, config)
+conf
 %0A   

handle config.fetch type error