"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getI18nModule = getI18nModule;

var _path = _interopRequireDefault(require("path"));

var _constants = require("./constants");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* eslint-disable no-underscore-dangle */
const i18nModuleCache = new WeakMap();

function getI18nModule(webpack) {
  if (i18nModuleCache.has(webpack)) {
    return i18nModuleCache.get(webpack);
  }

  class I18nModule extends webpack.Module {
    constructor({
      context,
      identifier,
      identifierIndex,
      content,
      // ## layer,
      // ## supports,
      // ## media,
      // ## sourceMap,
      assets,
      assetsInfo
    }) {
      super(_constants.MODULE_TYPE, context);
      this.id = '';
      this._context = context;
      this._identifier = identifier;
      this._identifierIndex = identifierIndex;
      this.content = content; // ## this.layer = layer;
      // ## this.supports = supports;
      // ## this.media = media;
      // ## this.sourceMap = sourceMap;

      this.assets = assets;
      this.assetsInfo = assetsInfo;
      this._needBuild = true;
    }

    size() {
      return this.content.length;
    }

    identifier() {
      return `i18n|${this._identifier}|${this._identifierIndex}`;
    }

    readableIdentifier(requestShortener) {
      return `i18n ${requestShortener.shorten(this._identifier)}${this._identifierIndex ? ` (${this._identifierIndex})` : ''}`;
    } // eslint-disable-next-line class-methods-use-this


    getSourceTypes() {
      return _constants.TYPES;
    } // eslint-disable-next-line class-methods-use-this


    codeGeneration() {
      return _constants.CODE_GENERATION_RESULT;
    }

    nameForCondition() {
      const resource = this._identifier.split('!').pop();

      const idx = resource.indexOf('?');

      if (idx >= 0) {
        return resource.substring(0, idx);
      }

      return resource;
    }

    updateCacheModule(module) {
      if (this.content !== module.content || // ## this.layer !== module.layer ||
      // ## this.supports !== module.supports ||
      // ## this.media !== module.media ||
      // ## this.sourceMap !== module.sourceMap ||
      this.assets !== module.assets || this.assetsInfo !== module.assetsInfo) {
        this._needBuild = true;
        this.content = module.content; // ## this.layer = module.layer;
        // ## this.supports = module.supports;
        // ## this.media = module.media;
        // ## this.sourceMap = module.sourceMap;

        this.assets = module.assets;
        this.assetsInfo = module.assetsInfo;
      }
    }

    needRebuild() {
      return this._needBuild;
    }

    needBuild(context, callback) {
      callback(undefined, this._needBuild);
    }

    build(options, compilation, resolver, fileSystem, callback) {
      this.buildInfo = {
        assets: this.assets,
        assetsInfo: this.assetsInfo,
        cacheable: true,
        hash: this._computeHash(compilation.outputOptions.hashFunction)
      };
      this.buildMeta = {};
      this._needBuild = false;
      callback();
    }

    _computeHash(hashFunction) {
      const hash = webpack.util.createHash(hashFunction);
      hash.update(this.content); // ## if (this.layer) {
      // ##   hash.update(this.layer);
      // ## }
      // ## hash.update(this.supports || '');
      // ## hash.update(this.media || '');
      // ## hash.update(this.sourceMap || '');

      return hash.digest('hex');
    }

    updateHash(hash, context) {
      super.updateHash(hash, context);
      hash.update(this.buildInfo.hash);
    }

    serialize(context) {
      const {
        write
      } = context;
      write(this._context);
      write(this._identifier);
      write(this._identifierIndex);
      write(this.content); // ## write(this.layer);
      // ## write(this.supports);
      // ## write(this.media);
      // ## write(this.sourceMap);

      write(this.assets);
      write(this.assetsInfo);
      write(this._needBuild);
      super.serialize(context);
    }

    deserialize(context) {
      this._needBuild = context.read();
      super.deserialize(context);
    }

  }

  i18nModuleCache.set(webpack, I18nModule);
  webpack.util.serialization.register(I18nModule, _path.default.resolve(__dirname, 'I18nModule-new'), null, {
    serialize(instance, context) {
      instance.serialize(context);
    },

    deserialize(context) {
      const {
        read
      } = context;
      const contextModule = read();
      const identifier = read();
      const identifierIndex = read();
      const content = read(); // ## const layer = read();
      // ## const supports = read();
      // ## const media = read();
      // ## const sourceMap = read();

      const assets = read();
      const assetsInfo = read();
      const dep = new I18nModule({
        context: contextModule,
        identifier,
        identifierIndex,
        content,
        // ## layer,
        // ## supports,
        // ## media,
        // ## sourceMap,
        assets,
        assetsInfo
      });
      dep.deserialize(context);
      return dep;
    }

  });
  return I18nModule;
}