"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const format = require("../utils/format.js");
const create = require("../utils/create.js");
const basic = require("../utils/basic.js");
const [name, bem, t] = create.createNamespace("picker");
const getFirstEnabledOption = (options) => options.find((option) => !option.disabled) || options[0];
function getColumnsType(columns, fields) {
  const firstColumn = columns[0];
  if (firstColumn) {
    if (Array.isArray(firstColumn)) {
      return "multiple";
    }
    if (fields.children in firstColumn) {
      return "cascade";
    }
  }
  return "default";
}
function findIndexOfEnabledOption(options, index) {
  index = format.clamp(index, 0, options.length);
  for (let i = index; i < options.length; i++) {
    if (!options[i].disabled) return i;
  }
  for (let i = index - 1; i >= 0; i--) {
    if (!options[i].disabled) return i;
  }
  return 0;
}
const isOptionExist = (options, value, fields) => value !== void 0 && options.some((option) => option[fields.value] === value);
function findOptionByValue(options, value, fields) {
  const index = options.findIndex((option) => option[fields.value] === value);
  const enabledIndex = findIndexOfEnabledOption(options, index);
  return options[enabledIndex];
}
function formatCascadeColumns(columns, fields, selectedValues) {
  const formatted = [];
  let cursor = {
    [fields.children]: columns
  };
  let columnIndex = 0;
  while (cursor && cursor[fields.children]) {
    const options = cursor[fields.children];
    const value = selectedValues.value[columnIndex];
    cursor = basic.isDef(value) ? findOptionByValue(options, value, fields) : void 0;
    if (!cursor && options.length) {
      const firstValue = getFirstEnabledOption(options)[fields.value];
      cursor = findOptionByValue(options, firstValue, fields);
    }
    columnIndex++;
    formatted.push(options);
  }
  return formatted;
}
function getElementTranslateY(element) {
  const { transform } = window.getComputedStyle(element);
  const translateY = transform.slice(7, transform.length - 1).split(", ")[5];
  return Number(translateY);
}
function assignDefaultFields(fields) {
  return basic.extend(
    {
      text: "text",
      value: "value",
      children: "children"
    },
    fields
  );
}
exports.assignDefaultFields = assignDefaultFields;
exports.bem = bem;
exports.findIndexOfEnabledOption = findIndexOfEnabledOption;
exports.findOptionByValue = findOptionByValue;
exports.formatCascadeColumns = formatCascadeColumns;
exports.getColumnsType = getColumnsType;
exports.getElementTranslateY = getElementTranslateY;
exports.getFirstEnabledOption = getFirstEnabledOption;
exports.isOptionExist = isOptionExist;
exports.name = name;
exports.t = t;
