import debug from 'debug';

import BrowserMsgr from './chrome';
import api from './chromeApi';
import ctor from '../constructor';
import logSeed from './logSeed';





const log = debug(`Brex:talker:${logSeed}`);

export default class Talker extends BrowserMsgr {
  constructor (appId) {
    log('constructor', appId);

    super(appId);
    this.cid = 1;
    this.api = api;
    this.ready = false;
    this.onReadyCbs = ctor.object();
  }

  send (msg, cb) {
    msg.cid = this.cid++;
    msg.ext = this.appId;
    msg.host = window.document.location.hostname;
    msg.url = window.document.location.href;
    msg.isFrame = (window !== top);

    log('send', msg);

    super.send(msg, cb);
  }


  sendAnswer (message, sender, sendResponse) {
    log('sendAnswer ', message, sender);

    if (this.ready) {
      log('sendAnswer Talker is ready');

      return this.answerByReason(message, sender, sendResponse);
    }

    this.onReadyCbs[`${this.appId}${this.cid}`] = () => {
      log(`sendAnswer when Talker set ready with cb key: ${this.appId}${this.cid}`);

      return this.answerByReason(message, sender, sendResponse);
    }
  }

  answerByReason (message, sender, sendResponse) {
    log('answerByReason %s', message.reason);

    switch (message.reason) {
      case 'get.modules':
        this.sendModulesByProps(message, sender, sendResponse);
        break;
      default:
        sendResponse({err: true, value: "Unauthorized"});
        break;
    }
  }

  sendModulesByProps (message, sender, sendResponse) {
    log('sendModulesByProps');

    let [key, ...modules] = this.config.raw;

    sendResponse({k: 0});

    // modules.forEach((m) => {
    //   for(let k in m) {
    //     if (k === 'f' && m[k])
    //   }
    // });
  }
}

@@ -1594,161 +1594,1130 @@
 is.c
-onfig.raw;%0A%0A    sendResponse(%7Bk: 0%7D);%0A%0A    // modules.forEach((m) =%3E %7B%0A    //   for(let k in m) %7B%0A    //
+fg.raw;%0A%0A%0A    // modules.forEach((m) =%3E %7B%0A    //   log(m);%0A    //   if (m.e) %7B%0A    //     this.testE(m, message, sendResponse);%0A    //   %7D else %7B%0A    //     if (m.i) %7B%0A    //       this.testI(m, message, sendResponse);%0A    //     %7D else %7B%0A    //       this.testH(m, message, sendResponse);%0A    //     %7D%0A    //   %7D%0A    // %7D);%0A%0A%0A    sendResponse(%7Bk: 0%7D);%0A  %7D%0A%0A  testE (module, message, sendResponse) %7B%0A    if (!ctor.regExp(module.e).test(message.host)) %7B%0A      return this.testI(module, message, sendResponse);%0A    %7D%0A%0A    sendResponse(%7Bresult: 0%7D);%0A  %7D%0A%0A  testI (module, message, sendResponse) %7B%0A    if (!ctor.regExp(module.i).test(message.url)) %7B%0A      return this.testH(module, message, sendResponse);%0A    %7D%0A%0A    sendResponse(%7Bresult: 0%7D);%0A  %7D%0A%0A  testH (module, message, sendResponse) %7B%0A    if (ctor.regExp(module.h).test(message.host)) %7B%0A      return this.testF(module, message, sendResponse);%0A    %7D%0A%0A    sendResponse(%7Bresult: 0%7D);%0A  %7D%0A%0A  testF (module, message, sendResponse) %7B%0A    if (message.isFrame) %7B%0A
      
+ 
 if (
-k === 'f' && m%5Bk%5D)
+module.f === 1 %7C%7C module.f === 2) %7B
 %0A    
-//   %7D%0A    // %7D);
+    return sendResponse(%7Bresult: 0%7D);%0A      %7D%0A    %7D else %7B%0A%0A    %7D
 %0A  %7D

test methods