"use strict";
/*
 * edge
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Props = void 0;
const utils_1 = require("@poppinss/utils");
const stringify_attributes_1 = __importDefault(require("stringify-attributes"));
const Template_1 = require("../Template");
/**
 * Class to ease interactions with component props
 */
class Props {
    constructor(props) {
        this[Symbol.for('options')] = { props };
        Object.assign(this, props);
    }
    /**
     * Merges the className attribute with the class attribute
     */
    mergeClassAttributes(props) {
        if (props.className) {
            if (!props.class) {
                props.class = [];
            }
            /**
             * Normalize class attribute to be an array
             */
            if (!Array.isArray(props.class)) {
                props.class = [props.class];
            }
            props.class = props.class.concat(props.className);
            props.className = false;
        }
        return props;
    }
    /**
     * Find if a key exists inside the props
     */
    has(key) {
        const value = this.get(key);
        return value !== undefined && value !== null;
    }
    /**
     * Get value for a given key
     */
    get(key, defaultValue) {
        return utils_1.lodash.get(this.all(), key, defaultValue);
    }
    /**
     * Returns all the props
     */
    all() {
        return this[Symbol.for('options')].props;
    }
    /**
     * Validate prop value
     */
    validate(key, validateFn) {
        const value = this.get(key);
        validateFn(key, value);
    }
    /**
     * Return values for only the given keys
     */
    only(keys) {
        return utils_1.lodash.pick(this.all(), keys);
    }
    /**
     * Return values except the given keys
     */
    except(keys) {
        return utils_1.lodash.omit(this.all(), keys);
    }
    /**
     * Serialize all props to a string of HTML attributes
     */
    serialize(mergeProps, priortizeInline = true) {
        /**
         * Priortize user attributes when priortizeInline=false
         */
        const attributes = priortizeInline
            ? utils_1.lodash.merge({}, this.all(), mergeProps)
            : utils_1.lodash.merge({}, mergeProps, this.all());
        return (0, Template_1.safeValue)((0, stringify_attributes_1.default)(this.mergeClassAttributes(attributes)));
    }
    /**
     * Serialize only the given keys to a string of HTML attributes
     */
    serializeOnly(keys, mergeProps, priortizeInline = true) {
        /**
         * Priortize user attributes when priortizeInline=false
         */
        const attributes = priortizeInline
            ? utils_1.lodash.merge({}, this.only(keys), mergeProps)
            : utils_1.lodash.merge({}, mergeProps, this.only(keys));
        return (0, Template_1.safeValue)((0, stringify_attributes_1.default)(this.mergeClassAttributes(attributes)));
    }
    /**
     * Serialize except the given keys to a string of HTML attributes
     */
    serializeExcept(keys, mergeProps, priortizeInline = true) {
        /**
         * Priortize user attributes when priortizeInline=false
         */
        const attributes = priortizeInline
            ? utils_1.lodash.merge({}, this.except(keys), mergeProps)
            : utils_1.lodash.merge({}, mergeProps, this.except(keys));
        return (0, Template_1.safeValue)((0, stringify_attributes_1.default)(this.mergeClassAttributes(attributes)));
    }
}
exports.Props = Props;
