"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var text_builder_exports = {};
__export(text_builder_exports, {
  TextBuilder: () => TextBuilder,
  ansi: () => ansi
});
module.exports = __toCommonJS(text_builder_exports);
var import_producer = require("./producer.js");
var import_sgr = require("./sgr.js");
var import_style = require("./style.js");
var import_text_writer = require("./text-writer.js");
/**
 * @license
 * Copyright 2022-2026 Greg Lauckhart <greg@lauckhart.com>
 * SPDX-License-Identifier: Apache-2.0
 */
function TextBuilder(definition, derivesFrom, state) {
  if (derivesFrom instanceof TextBuilder) {
    state = { ...derivesFrom.state, ...state };
  } else if (state === void 0) {
    state = {};
  }
  definition = import_style.Style.definitionOf(definition);
  const self = function TextBuilderInstance(...text) {
    const sequence = self.state.sequence ? [...self.state.sequence] : [];
    if (self.styleDefinition?.length) {
      sequence.push(self.styleToken);
    }
    sequence.push(...text);
    return TextBuilder(self, self, { sequence, revert: true });
  };
  Object.setPrototypeOf(self, TextBuilder.prototype);
  self.state = state;
  if (definition !== void 0) {
    self.styleDefinition = definition;
  }
  return self;
}
TextBuilder.prototype = Object.create(import_style.Style.prototype, {
  bg: chmode({ bg: true }),
  not: chmode({ not: true }),
  bold: toggle("bold"),
  dim: toggle("dim"),
  bright: chmode({ bright: true }),
  italic: toggle("italic"),
  underline: toggle("underline"),
  doubleUnderline: toggle("doubleUnderline"),
  slowBlink: toggle("slowBlink"),
  fastBlink: toggle("fastBlink"),
  invert: toggle("invert"),
  conceal: toggle("conceal"),
  strike: toggle("strike"),
  black: color("black"),
  red: color("red"),
  green: color("green"),
  yellow: color("yellow"),
  blue: color("blue"),
  magenta: color("magenta"),
  cyan: color("cyan"),
  white: color("white"),
  reset: {
    get() {
      return this.addStyle([0]);
    }
  },
  color: {
    value(color2) {
      if (this.state.not) {
        return this.addStyle([this.state.bg ? import_sgr.SgrOpcode.normalBg : import_sgr.SgrOpcode.normalFg]);
      }
      if (typeof color2 === "object") {
        if ("color" in color2) {
          return this.addStyle([import_sgr.Color.extended, import_sgr.ExtendedColor.eightBit, color2.color]);
        }
        return this.addStyle([import_sgr.Color.extended, import_sgr.ExtendedColor.rgb, color2.r ?? 0, color2.g ?? 0, color2.b ?? 0]);
      }
      const block = this.state.bg ? this.state.bright ? import_sgr.SgrOpcodeBlock.bgBright : import_sgr.SgrOpcodeBlock.bg : this.state.bright ? import_sgr.SgrOpcodeBlock.fgBright : import_sgr.SgrOpcodeBlock.fg;
      return this.addStyle([block + color2]);
    }
  },
  toString: {
    value() {
      let { sequence } = this.state;
      if (!sequence) {
        sequence = [this.styleToken];
      }
      if (this.state.revert) {
        sequence = [...sequence, this.undoStyle.styleToken];
      }
      const parts = Array();
      const writer = new import_text_writer.TextWriter((part) => parts.push(part));
      writer.write(...sequence);
      writer.close();
      return parts.join("");
    }
  },
  [Symbol.iterator]: {
    get() {
      if (this.state.sequence === void 0) {
        return {
          next() {
            return void 0;
          },
          done: true
        };
      }
      let sequence = this.state.sequence;
      if (this.state.revert) {
        sequence = [...sequence, this.undoStyle.styleToken];
      }
      const iterable = import_producer.Producer.of(sequence);
      return iterable[Symbol.iterator].bind(iterable);
    }
  },
  factory: {
    value: TextBuilder
  }
});
function chmode(state) {
  return {
    get() {
      return TextBuilder(this, this, state);
    }
  };
}
function toggle(name) {
  const on = import_sgr.SgrOpcode[name];
  const off = import_sgr.SgrOpcodeToUndoOpcode[on];
  if (off === void 0) {
    throw new Error(`Can't toggle opcode ${on}`);
  }
  return {
    get() {
      return this.addStyle([this.state.not ? off : on]);
    }
  };
}
function color(color2) {
  const opcode = import_sgr.Color[color2];
  return {
    get() {
      return this.color(opcode);
    }
  };
}
const ansi = TextBuilder();
//# sourceMappingURL=text-builder.js.map
