// Generated by IcedCoffeeScript 1.7.1-c
(function() {
  var Prompter, checkers, iced, log, read, strip, __iced_k, __iced_k_noop;

  iced = require('iced-runtime').iced;
  __iced_k = __iced_k_noop = function() {};

  read = require('read');

  checkers = require('./checkers').checkers;

  log = require('./log');

  exports.Prompter = Prompter = (function() {
    function Prompter(_fields) {
      this._fields = _fields;
      this._data = {};
    }

    Prompter.prototype.data = function() {
      return this._data;
    };

    Prompter.prototype.clear = function(k) {
      return delete this._data[k];
    };

    Prompter.prototype.run = function(cb) {
      var err, k, v, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      err = null;
      (function(_this) {
        return (function(__iced_k) {
          var _i, _k, _keys, _ref, _results, _while;
          _ref = _this._fields;
          _keys = (function() {
            var _results1;
            _results1 = [];
            for (_k in _ref) {
              _results1.push(_k);
            }
            return _results1;
          })();
          _i = 0;
          _results = [];
          _while = function(__iced_k) {
            var _break, _continue, _next;
            _break = function() {
              return __iced_k(_results);
            };
            _continue = function() {
              return iced.trampoline(function() {
                ++_i;
                return _while(__iced_k);
              });
            };
            _next = function(__iced_next_arg) {
              _results.push(__iced_next_arg);
              return _continue();
            };
            if (!(_i < _keys.length)) {
              return _break();
            } else {
              k = _keys[_i];
              v = _ref[k];
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/max/src/keybase/node-client/src/prompter.iced",
                  funcname: "Prompter.run"
                });
                _this.read_field(k, v, __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      return err = arguments[0];
                    };
                  })(),
                  lineno: 18
                }));
                __iced_deferrals._fulfill();
              })(function() {
                (function(__iced_k) {
                  if (err != null) {
                    (function(__iced_k) {
_break()
                    })(__iced_k);
                  } else {
                    return __iced_k();
                  }
                })(_next);
              });
            }
          };
          _while(__iced_k);
        });
      })(this)((function(_this) {
        return function() {
          return cb(err);
        };
      })(this));
    };

    Prompter.prototype.read_field = function(k, _arg, cb) {
      var checker, confirm, d, defval, err, first, first_prompt, hint, isDefault, normalizer, obj, ok, p, passphrase, prompt, res, res2, stderr, thrower, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      prompt = _arg.prompt, passphrase = _arg.passphrase, checker = _arg.checker, confirm = _arg.confirm, defval = _arg.defval, thrower = _arg.thrower, first_prompt = _arg.first_prompt, hint = _arg.hint, normalizer = _arg.normalizer, stderr = _arg.stderr;
      err = null;
      ok = false;
      first = true;
      (function(_this) {
        return (function(__iced_k) {
          var _results, _while;
          _results = [];
          _while = function(__iced_k) {
            var _break, _continue, _next;
            _break = function() {
              return __iced_k(_results);
            };
            _continue = function() {
              return iced.trampoline(function() {
                return _while(__iced_k);
              });
            };
            _next = function(__iced_next_arg) {
              _results.push(__iced_next_arg);
              return _continue();
            };
            if (!!ok) {
              return _break();
            } else {
              p = first ? prompt + (first_prompt != null ? first_prompt : "") + ": " : prompt + " (" + (hint || checker.hint) + "): ";
              first = false;
              obj = {
                prompt: p
              };
              if (stderr != null) {
                obj.output = process.stderr;
              }
              if (passphrase) {
                obj.silent = true;
                obj.replace = "*";
              }
              if (((d = _this._data[k]) != null) || ((d = defval) != null)) {
                obj["default"] = d;
                obj.edit = true;
              }
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/max/src/keybase/node-client/src/prompter.iced",
                  funcname: "Prompter.read_field"
                });
                read(obj, __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      res = arguments[1];
                      return isDefault = arguments[2];
                    };
                  })(),
                  lineno: 42
                }));
                __iced_deferrals._fulfill();
              })(function() {
                (function(__iced_k) {
                  if (err != null) {
                    (function(__iced_k) {
_break()
                    })(__iced_k);
                  } else {
                    return __iced_k();
                  }
                })(function() {
                  if (normalizer != null) {
                    res = normalizer(res);
                  }
                  (function(__iced_k) {
                    if ((thrower != null) && ((err = thrower(k, res)) != null)) {
                      return __iced_k(ok = true);
                    } else {
                      (function(__iced_k) {
                        if (((checker != null ? checker.f : void 0) != null) && !checker.f(res)) {
                          return __iced_k(ok = false);
                        } else {
                          (function(__iced_k) {
                            if ((confirm == null) || isDefault) {
                              return __iced_k(ok = true);
                            } else {
                              delete obj["default"];
                              obj.edit = false;
                              obj.prompt = confirm.prompt + ": ";
                              (function(__iced_k) {
                                __iced_deferrals = new iced.Deferrals(__iced_k, {
                                  parent: ___iced_passed_deferral,
                                  filename: "/Users/max/src/keybase/node-client/src/prompter.iced",
                                  funcname: "Prompter.read_field"
                                });
                                read(obj, __iced_deferrals.defer({
                                  assign_fn: (function() {
                                    return function() {
                                      err = arguments[0];
                                      return res2 = arguments[1];
                                    };
                                  })(),
                                  lineno: 56
                                }));
                                __iced_deferrals._fulfill();
                              })(function() {
                                return __iced_k(res2 !== res ? (ok = false, log.warn("Passphrases didn't match! Try again.")) : ok = true);
                              });
                            }
                          })(__iced_k);
                        }
                      })(__iced_k);
                    }
                  })(function() {
                    return _next(ok ? !isDefault || !(_this._data[k] != null) ? _this._data[k] = res : void 0 : void 0);
                  });
                });
              });
            }
          };
          _while(__iced_k);
        });
      })(this)((function(_this) {
        return function() {
          return cb(err);
        };
      })(this));
    };

    return Prompter;

  })();

  strip = function(s) {
    var m, x;
    x = /^\s*(.*?)\s*$/;
    if ((m = s.match(x)) != null) {
      s = m[1];
    }
    return s;
  };

  exports.prompt_yn = function(_arg, cb) {
    var ch, defval, err, obj, prompt, res, ret, win, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    prompt = _arg.prompt, defval = _arg.defval;
    win = process.platform === 'win32';
    if (win) {
      defval = null;
    }
    ch = defval != null ? "[" + (defval ? 'Y' : 'y') + "/" + (!defval ? 'N' : 'n') + "]" : '[y/n]';
    prompt += " " + ch + " ";
    obj = {
      prompt: prompt
    };
    ret = null;
    err = null;
    (function(_this) {
      return (function(__iced_k) {
        var _results, _while;
        _results = [];
        _while = function(__iced_k) {
          var _break, _continue, _next;
          _break = function() {
            return __iced_k(_results);
          };
          _continue = function() {
            return iced.trampoline(function() {
              return _while(__iced_k);
            });
          };
          _next = function(__iced_next_arg) {
            _results.push(__iced_next_arg);
            return _continue();
          };
          if (!((ret == null) && (err == null))) {
            return _break();
          } else {
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/max/src/keybase/node-client/src/prompter.iced",
                funcname: "prompt_yn"
              });
              read(obj, __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return res = arguments[1];
                  };
                })(),
                lineno: 85
              }));
              __iced_deferrals._fulfill();
            })(function() {
              return _next(err == null ? (res = strip(res), res.length === 0 ? defval != null ? ret = defval : void 0 : "yes".indexOf(res.toLowerCase()) >= 0 ? ret = true : "no".indexOf(res.toLowerCase()) >= 0 ? ret = false : void 0) : void 0);
            });
          }
        };
        _while(__iced_k);
      });
    })(this)((function(_this) {
      return function() {
        return cb(err, ret);
      };
    })(this));
  };

  exports.prompt_passphrase = function(_arg, cb) {
    var checker, confirm, err, extra, no_leading_space, p, prompt, seq, short, stderr, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    prompt = _arg.prompt, confirm = _arg.confirm, extra = _arg.extra, short = _arg.short, no_leading_space = _arg.no_leading_space, stderr = _arg.stderr;
    if (prompt == null) {
      prompt = "Your keybase login passphrase";
      if (extra != null) {
        prompt += extra;
      }
    }
    checker = short ? checkers.passphrase_short : no_leading_space ? checkers.passphrase_nls : checkers.passphrase;
    seq = {
      passphrase: {
        prompt: prompt,
        passphrase: true,
        checker: checker,
        confirm: confirm,
        stderr: stderr
      }
    };
    p = new Prompter(seq);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/max/src/keybase/node-client/src/prompter.iced",
          funcname: "prompt_passphrase"
        });
        p.run(__iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              return err = arguments[0];
            };
          })(),
          lineno: 115
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        return cb(err, p.data().passphrase);
      };
    })(this));
  };

  exports.prompt_remote_name = function(_arg, cb) {
    var checker, err, hint, p, prompt, seq, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    prompt = _arg.prompt, checker = _arg.checker, hint = _arg.hint;
    seq = {
      name: {
        prompt: prompt,
        checker: checker,
        hint: hint
      }
    };
    p = new Prompter(seq);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/max/src/keybase/node-client/src/prompter.iced",
          funcname: "prompt_remote_name"
        });
        p.run(__iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              return err = arguments[0];
            };
          })(),
          lineno: 123
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        return cb(err, p.data().name);
      };
    })(this));
  };

  exports.prompt_email_or_username = function(cb) {
    var err, out, p, seq, v, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    seq = {
      email_or_username: {
        prompt: "Your keybase username or email",
        checker: checkers.email_or_username
      }
    };
    p = new Prompter(seq);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/max/src/keybase/node-client/src/prompter.iced",
          funcname: "prompt_email_or_username"
        });
        p.run(__iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              return err = arguments[0];
            };
          })(),
          lineno: 134
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (typeof err !== "undefined" && err !== null) {
          out = {};
        } else {
          v = p.data().email_or_username;
          out = {
            email: (checkers.email.f(v) ? v : null),
            username: (checkers.username.f(v) ? v : null)
          };
        }
        return cb(err, out);
      };
    })(this));
  };

  exports.prompt_for_int = function(_arg, cb) {
    var d, defint, err, first_prompt, hi, hint, low, out, p, prompt, seq, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    prompt = _arg.prompt, low = _arg.low, hi = _arg.hi, defint = _arg.defint, hint = _arg.hint, first_prompt = _arg.first_prompt;
    seq = {
      key: {
        prompt: prompt,
        checker: checkers.intcheck(low, hi, defint),
        hint: hint,
        first_prompt: first_prompt
      }
    };
    p = new Prompter(seq);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/max/src/keybase/node-client/src/prompter.iced",
          funcname: "prompt_for_int"
        });
        p.run(__iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              return err = arguments[0];
            };
          })(),
          lineno: 153
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        out = typeof err !== "undefined" && err !== null ? null : (d = p.data().key) === '' && (defint != null) ? defint : parseInt(d);
        return cb(err, out);
      };
    })(this));
  };

}).call(this);
