import {
  runproteinpaint
} from "./chunk-74ZU4BT6.js";

// test/front.helpers.js
var serverData = /* @__PURE__ */ Object.create(null);
function getRunPp(appname = "", defaultArgs = {}, _host = "http://localhost:3000") {
  const host = window.testHost || _host;
  const arg = {
    host: window.testHost || _host,
    noheader: 1,
    nobox: true,
    debug: 1,
    norecover: true
  };
  if (appname == "mass" || appname == "termdb") {
    defaultArgs.debounceInterval = 0;
  }
  if (appname) arg[appname] = defaultArgs;
  else copyMerge(arg, defaultArgs);
  const argStr = JSON.stringify(arg);
  return function runpp(overrides = {}) {
    const argCopy = JSON.parse(argStr);
    if (appname) copyMerge(argCopy[appname], overrides);
    else copyMerge(argCopy, overrides);
    if (appname && defaultArgs.fetchOpts) {
      argCopy[appname].fetchOpts = defaultArgs.fetchOpts;
    }
    return runproteinpaint(Object.assign(argCopy, { serverData }));
  };
}
function copyMerge(base, ...args) {
  const target = typeof base == "string" ? JSON.parse(base) : base;
  for (const arg of args) {
    if (arg) {
      const source = typeof base == "string" ? JSON.parse(JSON.stringify(arg)) : arg;
      for (const key in source) {
        if (!target[key] || Array.isArray(target[key]) || typeof target[key] !== "object") target[key] = source[key];
        else copyMerge(target[key], source[key]);
      }
    }
  }
  return target;
}
function rideInit(opts = {}) {
  const self = new Ride(opts);
  const rideApi = {
    // ride on the default event bus
    to(callback, sub = {}) {
      self.addToThen(callback, Object.assign({}, opts, sub));
      return rideApi;
    },
    use(triggerFxn, sub = {}) {
      self.addUseThen(triggerFxn, Object.assign({}, opts, sub));
      return rideApi;
    },
    change(sub = {}) {
      Object.assign(opts, sub);
      return rideApi;
    },
    // run callback without using bus, with or without timeout
    run(callback, after = 0) {
      self.addRunThen(callback, after, opts);
      return rideApi;
    },
    // close the event bus
    done(test) {
      if (opts.bus) self.resolved.then(() => opts.bus.on(opts.eventType, null));
      self.resolved.then(() => {
        test.end();
        if (!opts.preserve && test._ok && opts.arg.Inner.app && typeof opts.arg.Inner.app.destroy == "function") {
          opts.arg.Inner.app.destroy();
        }
      }).catch(console.log);
      return rideApi;
    }
  };
  return Object.freeze(rideApi);
}
var Ride = class {
  // mutable but private access props and methods
  // when used inside rideInit()
  constructor(opts) {
    this.validateOpts(opts);
    this.resolved = Promise.resolve();
  }
  validateOpts(opts) {
    try {
      if (opts.eventType && !opts.bus) {
        if (!opts.arg) throw "must specify opts.bus or opts.arg for rideInit() argument";
        opts.bus = opts.arg;
      }
      if (opts.bus) {
        if (typeof opts.eventType !== "string") throw "invalid default.eventType";
        opts.bus.on(opts.eventType, null);
        return opts;
      }
    } catch (e) {
      console.log(e);
    }
  }
  addToThen(callback, opts) {
    this.resolved = this.resolved.then(async (triggerFxn) => {
      opts.bus.on(opts.eventType, null);
      return new Promise(async (resolve, reject) => {
        opts.bus.on(opts.eventType, async () => {
          await sleep(opts.wait);
          callback(opts.arg);
          resolve();
        });
        if (triggerFxn) await triggerFxn();
      });
    });
  }
  // prepare a trigger function for use in the pattern
  // rideInit().use(triggerFxn).to(...)
  //
  // .use() enables setting a different opts.arg to be used for triggerFxn
  //
  addUseThen(triggerFxn, opts) {
    this.resolved = this.resolved.then(async (prevTriggerFxn) => {
      if (typeof prevTriggerFxn == "function") await prevTriggerFxn();
      await sleep(isNaN(opts.wait) ? 0 : opts.wait);
      return () => triggerFxn(opts.arg);
    });
  }
  addRunThen(callback, after, opts) {
    this.resolved = this.resolved.then(async () => {
      await sleep(typeof after == "number" ? after : 0);
      callback(opts.arg);
    });
  }
};
function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

export {
  getRunPp,
  rideInit
};
//# sourceMappingURL=chunk-W55XSG6K.js.map
